Introduction:
After a couple of years of experience, I tried different ways to deal with beans and pages. There are many ways to deal with this. Most of the times I used JSTL, Freemarker, Velocity or the spring Taglibs. Now i am trying to get clear what is the best way of dealing I can use.
Why jstl:
I used jstl a lot for the coupling of the beans and the jsp's. This still gives you the freedom to use also the java <%%> tags. In my opinion it is not wise to do this simply of the fact, that you run into the risk of starting to use business logic in the jsp's which is straight against the mvc pattern and the multi tier architecture. The only real downside of the JSTL's for me: is that for different functions you need different libraries declared in your jsp:
- Core tags
- Formatting tags
- Sql tags
- xml tags
The only taglib I do not like to use is the sql tags. I believe that this kind of functionality should be left to handled by the persistence tier.
The conclusion for using JSTL is: It is a very usefull set of libraries which can help you to create a good separation of your model and view. It still offers a lot of freedom which should be handled thoughtfully. In an development tool as Eclipse you might experience that this tool does not see it as on the classpath and gives compile errors in that sense. At runtime this example will work.
An examples:
//This is the jstl import
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
//Looping through a list of names in the items you have//
//to declare the full path. In this case it a list in the customer class.//
<c:forEach var="costumerNames" items="${com.my.customer.names}">
//printing the names in al HTML list.//
<LI> <c:out value="${customerNames.name}"/> </LI>
</c:forEach>
Why velocity
Velocity brings you the freedom to use their templates only or embed them in a Jsp. The bridging between the model and view. The plus of Velocity against the JSTL is that it works in different areas of your application. You can as easily use it for pages as for the layout for automated emails. It has a lot more functionality which makes it much more flexible to work with. Instead of bringing extra technology into the Jsp, there is an template created a .vm file, wich is compiled into a view.
The conclusion:
If there is a need for a lot of flexibility even in a legacy system where there are allready Jsp's velocity is a flexible tool.
- It adapts to many application areas
- It offers a simple, clear syntax for the template designer
- It offers a simple programming model for the developer
- Because templates and code are separate, you can develop and maintain them independently
- The Velocity engine easily integrates into any Java application environment, especially servlets
- Velocity enables templates to access any public method of data objects in the context
an example for a web app:
The servlet.xml example:
<!-- register servlet
dont take this part to letterly. This is als depending on the frontend
framework you are using.
-->
<servlet> <servlet-name>applicationServlet</servlet-name> <servlet-class>my.velocity.VelocityServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>applicationServlet</servlet-name> <url-pattern>/myApplication</url-pattern> </servlet-mapping> <!-- mapping all .vm files to velocity servlets --> <servlet> <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class> <!-- load my toolbox --> <init-param> <param-name>org.apache.velocity.toolbox</param-name> <param-value>/WEB-INF/toolbox.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping>
The toolbo.xml example (place it in the WEB-INF):
<toolbox> <tool> <key>date</key> <scope>application</scope> <class>org.apache.velocity.tools.generic.DateTool</class> </tool> </toolbox>
The java part:In this part you have to create a bean with a member "name" that has getters and setters. With whatever framework you use as frontend technology (serlets, struts,spring, etc...) set this as a request parameter with the name "name". This,
ofcourse comes back in the html.
The html part:<h2> $name is using velocity</h2>
Why freemarker
Freemarker is an template engine, which might be seen as simular to Velocity. The difference in what they
can do sits in the functionality they have. If you have light requirements Velocity is the answer
to your question on the other hand if you like one of the following functionalities out of the box
you might consider using Freemarker:
Number and date support
|
An example:
The java class:
//Freemarker configuration object
Configuration cfg =
new
Configuration();
try
{
//Load template from source folder
Template template = cfg.getTemplate(
"src/helloworld.ftl"
);
// Build the data-model
Map<String, Object> data =
new
HashMap<String, Object>();
data.put(
"message"
,
"Hello World!"
);
//List parsing
List<String> countries =
new
ArrayList<String>();
countries.add(
"India"
);
countries.add(
"United States"
);
countries.add(
"Germany"
);
countries.add(
"France"
);
data.put(
"countries"
, countries);
// Console output
Writer out =
new
OutputStreamWriter(System.out);
template.process(data, out);
out.flush();
// File output
Writer file =
new
FileWriter (
new
File(
"C:\\FTL_helloworld.txt"
));
template.process(data, file);
file.flush();
file.close();
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(TemplateException e) {
e.printStackTrace();
}
}
The ftl Code:
<#list countries as country>
${country_index + 1}. ${country}
</#list>
The output:
1. India
2. United States
3. Germany
4. France
I hope this helps to choose the tool you need. I started out a new project and decided to use the jstl and the spring taglibs. They provided me with all I need.
But if I need something heavier the next time I might use Velocity or Freemarker to suit my needs.
Have fun!