Generate html code which depends on a parameter from java
I want to be able to generate html pages where some parts of the html should depend on some parameters.
My idea is that I could have a template like OneMailTemplate.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MailTemplate</title>
<meta http-equiv="Content-Type" co开发者_运维百科ntent="text/html; charset=UTF-8">
</head>
<body>
<p>Bla bla bla</p>
<someComponent>
<p>Bla bla</p>
</body>
</html>
and then get some method to replace the tag
<someComponent>
with some generated html code. Is there a way to do this in java without the use of StringTokenizer?
I am also open to suggestions about how I can do this in another way.As much as I like XSLT, I think it may be a bit heavy for this purpose. Freemarker is made for this kind of thing. It can take an HTML template with parameters like ${name} in the html and create your output without being dependent upon a jsp.
It's kind of overkill to use a Java program to do this...
But the proper way in Java would be to use DOM functions to find the tag, create your replacement subtree, and output the result.
If your replacement is static, you should use XSLT instead. If the replacement is dynamic, consider using JSP instead of a custom preprocessor - JSP is designed for this case.
I use Velocity for my mail templates.
精彩评论