3

I am working on a functionality where the application needs to generate user specific emails. This will be setup or configured on the user level using a email template which essentially contains a SQL query, column model, data type, subject, header, footer etc. The template serves as the dataset and layout for the email.

Now using this XML template I need to generate the HTML email. The application will read the XML, execute the SQL query and then match the resultset to the column model. Beyond this; is there any framework or API that can help generate the HTML response (nicely formatted css table) from Java objects or it has to be cooked using raw HTML tags (, etc.)?

I was also researching to see if BIRT or Jasper can provide HTML response but it doesn't seem like they are meant for that. If anyone has experience building a solution for such a use case please let me know.

Anand Nadar
  • 236
  • 1
  • 6
  • 14
  • With "HTML response from Java objects", do you mean XML serialized java objects? I would use something like [XSLT](http://www.w3schools.com/XSL/) to transform the XML into HTML. – Mathijs Flietstra Apr 15 '13 at 21:01
  • Jasper can generate HTML as far as I know. At least iReport has an option for it. – Sotirios Delimanolis Apr 15 '13 at 21:02
  • I see that Jasper has JRHtmlExporter which could be of use. But also hearing from people that there is less control on the styling in the HTML output. Extending might be an option however it might be limited. – Anand Nadar Apr 15 '13 at 21:32

2 Answers2

11

Take a look at Thymeleaf. It's a HTML template engine.

It's as simple as this:

ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode("HTML5");
resolver.setSuffix(".html");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);
final Context context = new Context(Locale.CANADA);
String name = "John Doe";
context.setVariable("name", name); 
// add more objects from your ResultSet

final String html = templateEngine.process("myhtml", context);

with a myhtml.html file:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-3.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>My first template with Thymeleaf</title>
</head>
<body>
    <p th:text="${name}">A Random Name</p> 
</body>
</html>

Here the placeholder ${name} will replace the value A Random Name in the <p> element by the value you inserted in the context.

As for your requirement of reading and generating a table, Thymeleaf provides constructs to loop as many times as is required (ie. as long as you have data remaining). Example:

<tr th:each="prod : ${allProducts}">

will iterate through allProducts, assigning each object to the variable prod at each iteration. Take a look at the tutorials and the docs for more.

Notice, you have to write the HTML yourself.

Take a look at this answer for generating HTML report through Jasper

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I like Thymeleaf from the examples. Still need to serialize the resultset to HTML. Thought that there will be boilerplate code to perform these trivial things. – Anand Nadar Apr 15 '13 at 21:49
  • @AnandNadar You don't serialize the `ResultSet`. You extract the data, put it in the context with the proper key, and reference it in the HTML. – Sotirios Delimanolis Apr 15 '13 at 22:05
-1

You can use XSLT to transform your XML to HTML. The result of your SQL query would have to be inserted as XML beforehand.