Stripes is a JDK 1.5 based framework for building web
applications. What makes this framework stand apart from the rest of the java
based frameworks is the fact that it does not require extensive configuration
other than adding entries for a 'Dispatcher Servlet' and filters in the
deployment descriptor. The latest release (Stripes 1.3) also provides
'Spring' and '
AJAX
' integration. That way it can truly help with building low bandwidth web
applications. The good thing about Stripes is that it is simple and hence easy
to use. It uses JDK1.5, Servlet 2.4 and JSP 2.0. All you need to run a Stripes
based application is a Servlet container that supports these versions. In this
article we provide a small sample application that displays a greeting message
to describe how to get started with using this framework for your web
application.
|
How it works
Stripes' presentation layer consists of web pages that can be defined
using JSP. These JSP pages can import the Stripes tag-library using the
following declaration in the JSP file:
The action elements in these JSP pages forward the request
to 'ActionBean'. This is the heart of all properties of the action element
and processing logic in a Stripes application. This is a major difference with
other lightweight component based frameworks where the same class or object at
runtime handles both these tasks. Moreover you do not need any external
configuration for defining the 'ActionBean' implementation in an application
or for binding the JSP page request to the 'ActionBean'. This information is
defined in the 'ActionBean' itself.
Setting it up
You need to download the Stripes framework package from the URL
http://stripes.mc4j.org/confluence/display/stripes/Download and unzip it to a
location of your choice on the hard drive. Thereafter you need to add the path
for all the 'jars' that you find in the 'lib' directory of your Stripes
installation to the 'classpath' variable.
Configuring a Stripes application
Configuration is minimal in Stripes; we first start by configuring the
'web.xml' file to define a 'Stripes Filter' and a 'Stripes
Dispatcher' Servlet. These entries will be as follows:
net.sourceforge.stripes.controller.StripesFilter
Servlet
net.sourceforge.stripes.controller.DispatcherServlet
As you can see from the deployment descriptor listed above
you do not need to configure any thing else except the Stripes Filter and the
Servlet mappings. Next you need to define 'log4j', 'commons-logging' and
a 'StripesResources' properties file. The first two contain standard entries
for a web application whereas a StripesResources file defines error messages for
defined validations and the file has to be available in at the folder specified
by CLASSPATH. Also setup a context path entry 'Stripes-PCQ' in
'context.xml'
Building the JSP Page
Our application's JSP page displays a form asking for entering name, age
and sex. After that, it prints a display message with the values provided. The
JSP page is as follows:
Stripes Demo PCQ
Enter your Name | ||||
Enter your Name | ||||
Enter your Name | ||||
Srtipes Greets | ${actionBean.greet} |
The 'ActionBean' Implementer
The ActionBean we described earlier is an interface that implemented for
application specific goals in Stripes. Here we implement this interface
overriding two of its methods-'getActionBeanContext()' and 'setActionBeanContext()'.
The ActionBean implementation is as follows:
package com.pcq.stripes;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
public class HelloWorldBean implements ActionBean {
private ActionBeanContext context;
private String usrName;
private int age;
private String sex;
private String greet;
public ActionBeanContext getContext() { return context; }
public void setContext(ActionBeanContext context) {
this.context = context; }
public String getusrName() { return usrName; }
public void setusrName(String usrName) { this.usrName =
usrName; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public String getGreet(){
return greet;
}
public void setGreet(String greet){
this.greet = greet;
}
public void setGreet(String name, String
sex){
if(sex.equals("Male") ||
sex.equals("male") || sex.equals("MALE"))
greet =
"Hello Mr. "+name;
else if(sex.equals("Female")
|| sex.equals("female") || sex.equals("FEMALE"))
greet =
"Hello Ms. "+name;
else greet = "Hello "+name;
}
@DefaultHandler
public Resolution showGreet() {
setGreet(this.getusrName(),this.getSex());
return new ForwardResolution("/Stripes-PCQ/index.jsp");
}
These methods provide the ActionBean with access to the
ActionBeanContext, which provides access to the HttpServlet Request and
HttpServletResponse, as well as other information about the current request.
Most of the code is pretty obvious except for a '@DefaultHandler'.
This annotation describes application behavior if it cannot
find out what button the user hit (in case you press enter key!), which in our
case is to do the same, i.e. print the message. The Resolution Stripes will
identify the showGreet() method as a 'handler' method. When a request comes
to the HelloWorldBean, and the user hits a submit button with name (not value)
"showGreet", this method will be invoked. Just like with the URL
above, the name of the event that a method handles can be overridden using the @HandlesEvent
annotation.
With all the files defined you need to build the WAR
archive
using any tool and simply deploy it to a Servlet container
or application server of your choice. The deployment procedure would vary from
server to server. Access the following URL to view the JSP page:
http://localhost:8080/Stripes-PCQ/index.jsp
In conclusion
The framework is pretty easy to use and setup. It has some good design
features such as the 'ActionBean' we described and is extensible to
AJAX
. It can surely be a one-stop shop for developers looking for resource friendly
web applications that can be coded and deployed without much fuss.
Anadi Misra