Stripes and JSP tag files
My current project is to develop a backend system in Java, with a remote interface and a normal web application. At the moment I’m writing the web application.
Before I started I had to decide if I use a web application framework, or just use standard JSP and write my own dispatcher Servlet. In the last couple of years I worked with so many different technologies like Struts, WebWork, JSF, Bea WebLogic Portal, but none of them satisfied my completely. The best of them was WebWork.
But then I got the hint from a friend to try Stripes. I had a look at the quick start guide, and was pretty amazed. Everything looked so easy, and best of all, so little configuration is required. Stripes uses Java Annotations and conventions quite intelligent to configure and manage the web application. To get a feeling how easy it is, here a short example. Think about a web page that contains a form, where you can enter your email address to receive newsletters.
All you have to do is to write a class that extends ActionBean and forward to a JSP. ActionBeans are similar to Action classes in Struts, but with some usefull extensions. For instance parameters are automatically populated by Stripes, and validation of parameters can be configured through Annotation.
Example: NewsletterActionBean.java
package com.company.project.action;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.action.ActionBeanContext;
public class NewsletterActionBean extends ActionBean {
@Validate(required=true) private String email;
public Resolution subscribe() {
NewsletterService.subscribe(email);
return new ForwardResolution("/newsletter.jsp");
}
// Bean getters and setters
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// Usually moved to a BaseActionBean class
private ActionBeanContext context;
public ActionBeanContext getContext() {
return context;
}
public void setContext(ActionBeanContext context) {
this.context = context;
}
}
That’s all. The email is injected and validated by the Stripes framework, the subscribe method stores the email in the NewsletterService, and forwards to the newsletter.jsp page for rendering the HTML.
The next technology I have to mention are JSP tag files. Introduced in the JSP 2.0 specification it provides an alternative mechanism to write tag libs. I don’t know why this is not mentioned more often, because it’s really a cool extension and I use it quite heavily. You don’t have to write a Java class file. The only thing that is required, is a tag file (*.tag) that is placed in WEB-INF/tags (or META-INF/tags or in a JAR in WEB-INF/lib), and use it in the JSP file.
Example: index.jsp
...
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>
...
<html>
<head><title>Some Title</title></head>
<body>
<ui:header id="header">
<a href="http://www.company.com">
<img src="logo.png" />
</a>
</ui:header>
....
</body>
</html>
Example: header.tag
<%@ tag language="java" %>
<%@ attribute name="id" required="true" %>
<div id="${id}">
<jsp:dobody/>
<div id="headerMenu">
<ul> ... </ul>
</div>
</div>
Is pretty simple
With this tag files you can do quite a lot. One possibility is to create GUI components for your project. E.g. if you have a message dialog with rounded corners that has to be placed on many places, then you can create a tag called: message.tag and use it in the JSP.
Example: message.tag
...
<ui:message>
Place message dialog text here.
</ui:message>
...