Stripes - Hello, World!

Stripes is an open source web-application framework used to simplify Java web development. Stripes provides both View and Controllers of MVC architecture and let the developers to port their Model into the system. Comparing to Struts, Stripes is a lightweight and less configuration needed framework, but it provides almost everything provided by Struts. More details about Stripes can be found here.

This tutorial shows you; how to develop a simple web application using Stripes framework in Eclipse.

Prerequisite:
  • Java Development Kit 1.5 or latest
  • Eclipse JEE
  • Apache Tomcat (To add Apache Tomcat to Eclipse, follow this link.)
  • Stripes libraries and other dependencies
  • Basic knowledge in Java Servlets and JSP.
Create a new Dynamic Web Project
Step 1:
In Eclipe goto File → New → Project… and select Dynamic Web Project


Step 2:
Give the project name “Hello World” and click on Next.


Step 3:
Leave the output folder with default values and click Next.


Step 4:
Select the “Generate web.xml deployment descriptor” and click on the Finish button.


Download libraries and add them to the project
Step 5:
Download the Stripes library from this link.
Download the Apache-Commons dependency from this link.

Step 6:
Copy the downloaded JAR files to the lib folder of WEB_INF directory in your project.


Create ActionBean class
Step 7:
Create a new package under the src folder with a name: “com.javahelps.stripes.helloworld.action”.


Step 8:
Create a new class “TimeActionBean” inside that package.
package com.javahelps.stripes.helloworld.action;

import java.util.Date;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

public class TimeActionBean implements ActionBean {
 private static final String VIEW = "/WEB-INF/jsp/result.jsp";
 private ActionBeanContext context;
 private Date time;

 @Override
 public ActionBeanContext getContext() {
  return this.context;
 }

 @Override
 public void setContext(ActionBeanContext ctx) {
  this.context = ctx;
 }

 public Date getTime() {
  return this.time;
 }

 @DefaultHandler
 public Resolution currentTime() {
  this.time = new Date();
  return new ForwardResolution(VIEW);
 }
}
Here the ActionBean interface is used to define the TimeActionBean class as an ActionBean. At runtime, Stripes searches for ActioBean classes in a given package and use them to handle the received requests.

getContext and setContext methods are abstract methods define in the ActionBean interface, so the sub classes must override them.

currentTime is an event handler, and you can use any name for that. @DefaultHandler specifies that, any call to TimeActionBean without specifying an event name, executes the currentTime method by default.

An event handler:
  • Must have public access modifier
  • Should not take any parameters
  • Should return a Resolution
  • Should be defined inside an ActionBean
In this code currentTime method assigns the current time to the instance variable "time" and forward the request to result.jsp which is inside "/WEB-INF/jsp” folder. (Still we have not created it). result.jsp can read the time using getTime method.

Create JSP file.
Step 9:
Create a new directory “jsp” under the WEB-INF directory, and create a new result.jsp file inside that folder.


Stripes does not recommend allowing direct access to JSP files, even though it is allowed to access. This is the reason to create JSP file inside WEB-INF folder, since clients cannot directly access any resources which are inside the WEB-INF folder.

Step 10:
Modify the content of result.jsp as provided here.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
 <h1>Current time is: ${actionBean.time}</h1>
 <br/>
 <s:link beanclass="com.javahelps.stripes.helloworld.action.TimeActionBean" event="currentTime">
  Update the Time
 </s:link>
</body>
</html>
In this code, <%@taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld"%> is used to import the Stripes library into the JSP.
<s:link> is a Stripes specific attribute which is used to create link to a given ActionBean class. The beanClass is used to provide the target ActionBean. Instead of class name, href reference also can be used as show below.
<s:link href="/Time.action" event="currentTime">
 Update the Time
</s:link>
However, using the actionBean with class name is recommend, because it clearly shows that which class is going to handle the request.
The event is the method name which is declared in the TimeActionBean class.

JSP uses an ${actionBean.time} expression language; the actionBean is a reference to the TimeActionBean which will be assigned by the ActionBean when it forwards the request to JSP. ${actionBean.time} gets the assigned time and display it in the page.

Configure The Stripes Framework
Step 11:
Open the web.xml and replace its content by this code.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>Hello World</display-name>
 <filter>
  <filter-name>StripesFilter</filter-name>
  <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
  <init-param>
   <param-name>ActionResolver.Packages</param-name>
   <param-value>com.javahelps.stripes.helloworld.action</param-value>
  </init-param>
 </filter>
 <servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <filter-mapping>
  <filter-name>StripesFilter</filter-name>
  <servlet-name>DispatcherServlet</servlet-name>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
 </filter-mapping>

 <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.action</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
  <welcome-file>Time.action</welcome-file>
 </welcome-file-list>
</web-app>
Deployment descriptor of the web application is used to configure the Stripes framework. The filter's  init-param value of ActionResolver.Packages is the package where Stripes searches for ActionBean classes. In this project the ActionBean class TimeActionBean is inside the com.javahelps.stripes.helloworl.action package which is provided as the param-value. <welcome-file> uses the TimeActionBean as the welcome file.

URL of an ActionBean class is determined from the class name according to the following rules.

1. Start with the fully qualified class name of the action bean.
        com.javahelps.stripes.helloworld.action.TimeActionBean
2. Remove the package prefix up to and including any of the following package names: action, stripes, web, or www.
        TimeActionBean
3. If present, remove the class name suffix Action, Bean, or ActionBean.
        Time
4. Convert the package and class name to a path by prefixing with a forward slash and changing all periods to forward slashes.
        /Time
5. Append the .action suffix.
        /Time.action

Step 12:
Right click on the project and select Run As → Run on Server. Select Tomcat server and click next.


Step 13:
Make sure the application is listed under Configured applications and click on the Finish button.


Step 14:
Click on the “Update the Time” link and see the output.


Find the project at Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *