Examples of generating HTML via com-hfg objects.

The HTML classes in the com-hfg library are designed to let you construct complex html content for web applications in a modular, object-oriented way. This approach can be used in conjunction with javascript frameworks by constructing complex panels or forms on the server side and deploying them via AJAX. Say goodbye to unbalanced tags or html typos!

  1. Hello World
  2. A simple HTML table

Example 1: Hello World

  1. HTMLDoc htmlDoc = new HTMLDoc();
  2.  
  3. HTML html = (HTML) htmlDoc.getRootNode();
  4.  
  5. html.getBody().addDiv("Hello World");
  6.  
  7. htmlDoc.toIndentedHTML(3, 3, response.getWriter());
would produce the following HTML:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  6. </head>
  7. <body>
  8. <div>Hello World</div>
  9. </body>
  10. </html>

Example 2: A simple HTML table

In which a table is created and the available method chaining is demonstrated:
  1. public HTMLTag generateSimpleTable()
  2. {
  3. Table table = new Table().addClass("myTableClass");
  4. Tr row = table.addRow();
  5. row.addCell("BOLD text").addStyle(CSS.BOLD);
  6. row.addCell().addSpan("span in a cell");
  7.  
  8. return table;
  9. }

Return to Main Page