Introduction to Stripes

This is an introductory Stripes tutorial. We create two simple web applications with the Stripes web framework. We use NetBeans to build the applications.

Stripes is an open source lightweight Java web application framework. The goal of Stripes is to make Servlet/JSP based web development in Java easy, intuitive, and straightforward. Stripes is an action based MVC (Model View Controller) framework. It runs in a JEE web container, uses minimum configuration files, and has a flexible and simple parameter binding.

Stripes' ActionBean is an object that receives the data submitted in requests and processes the user's input. It both defines the properties of the form and the processing logic for the form. Stripes auto-discovers ActionBeans at deployment time by scanning the web application's classpath. The ActionResolver.Packages init-param of the Stripes Filter (in web.xml) sets one or more package roots.

Resolution is an object created as a response to the processed request. A Resolution can forward to a JSP page, stream data, or return an error message. Resolutions are returned by handler methods of ActionBeans.

From the Stripes' Github page, we download the latest Stripes release. In the lib subdirectory, we have the three JAR files that we need to include in our projects: commons-logging-1.1.3.jar, cos-05Nov2002.jar, and stripes-1.6.0.jar. In addition, there is also the StripesResources.properties file which contains various messages.

Simple Stripes application

The first application displays the current date. We create a new web application in NetBeans. We choose Tomcat as our JSP/servlet cointainer.

The project files
Figure: The project files

The project has three files: the HelloActionBean.java contains code which responds to our request, the showDate.jsp is the view sent as a response back to the user, and the web.xml file contains configuration to set up Stripes. In this application, we do not use StripesResources.properties.

The project libraries
Figure: The project libraries

These are the libraries that we need to build the Stripes application.

web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <filter>
        <display-name>Stripes Filter</display-name>
        <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.zetcode.action</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>StripesFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
 
    <filter-mapping>
        <filter-name>StripesFilter</filter-name>
        <servlet-name>StripesDispatcher</servlet-name>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
 
    <servlet>
        <servlet-name>StripesDispatcher</servlet-name>
        <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>StripesDispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>    
    
    <welcome-file-list>
        <welcome-file>Hello.action</welcome-file>
    </welcome-file-list>

</web-app>

In the standard web.xml deployment descriptor, we configure Stripes. We specify where Stripes should look for ActionBeans: in our case it is the com.zetcode.action package. The welcome file is the file that is displayed when we request the home page; the Hello.action tells that the HelloActionBean should be executed.

HelloActionBean.java
package com.zetcode.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 HelloActionBean implements ActionBean {

    private static final String VIEW = "/WEB-INF/jsp/showDate.jsp";
    private ActionBeanContext context;
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void setContext(ActionBeanContext context) {

        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {

        return context;
    }

    @DefaultHandler
    public Resolution hello() {
        
        this.date = new Date();
        return new ForwardResolution(VIEW);
    }
}

The HelloActionBean processes the requests and responds with a forward resolution to a JSP page.

private static final String VIEW = "/WEB-INF/jsp/showDate.jsp";

The view is the showDate.jsp file.

private ActionBeanContext context;

ActionBeanContext encapsulates information about the current request. It provides access to the underlying Servlet API should we need to use it for any reason.

@DefaultHandler
public Resolution hello() {
    
    this.date = new Date();
    return new ForwardResolution(VIEW);
}

The @DefaultHandler annotation sets the default handler for this action bean. It fills the date property with current date and returns a new ForwardResolution. The resolution forwards to the view.

showDate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Current date</title>
    </head>
    <body>
        <h3>The date is ${actionBean.date}</h3>
    </body>
</html>

This is the template view for the user. The ${actionBean} expression refers to the action bean that pointed to this view. We use the expression to refer to the action bean's date property.

$ curl localhost:8084/SimpleStripes/

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Current date</title>
    </head>
    <body>
        <h3>The date is Thu Jun 02 14:13:01 CEST 2016</h3>
    </body>
</html>

After we build and deploy the application, we access the home page of the application with the curl tool. The application responds with an HTML page that contains the current date.

Hello Stripes application

In the second application, we have a simple HTML form. The user specifies his name in a text box. The application responds with a greeting. Validation is used to ensure that the user has entered something into the text field.

web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
   
    <filter>
        <display-name>Stripes Filter</display-name>
        <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.zetcode.action</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>StripesFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
 
    <filter-mapping>
        <filter-name>StripesFilter</filter-name>
        <servlet-name>StripesDispatcher</servlet-name>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
 
    <servlet>
        <servlet-name>StripesDispatcher</servlet-name>
        <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>StripesDispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>    
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>

In the web.xml file, we set the index.jsp file to be the welcome file.

index.jsp
<%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Enter your name</title>
    </head>
    <body>
        <stripes:form beanclass="com.zetcode.action.HelloActionBean">
            <stripes:errors/>
                Enter your name:
                <stripes:text name="userName"/>
                <stripes:submit name="save" value="Submit"/>
        </stripes:form>
    </body>
</html>

The index.jsp contains a simple HTML form. Stripes has its own tags. The <stripes:errors/> displays validation errors. If we do not write any text into the field, a validation error is shown. In the <stripes:form> tag we specify the action bean which should process the request. The <stripes:text/> creates a text field. The created request parameter is automatically mapped to the userName property of the HelloActionBean.

StripesResources.properties
Figure: StripesResources.properties

StripesResources.properties is the default resource bundle for the Stripes framework. It contains values for various messages and labels. A sample file is included in the lib subdirectory of the Stripes download file. We put the file into the source packages, with no package specified. (The file should end up in the WEB-INF/classes directory.)

StripesResources.properties
...
validation.required.valueNotPresent={0} is a required field
...

This error message is shown when we do not enter anything into the text field and click on the Submit button.

HelloActionBean.java
package com.zetcode.action;

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;
import net.sourceforge.stripes.validation.Validate;

public class HelloActionBean implements ActionBean {

    private static final String VIEW = "/WEB-INF/jsp/greet.jsp";
    private ActionBeanContext context;
    
    @Validate(required=true)
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public void setContext(ActionBeanContext context) {

        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {

        return context;
    }

    @DefaultHandler
    public Resolution greet() {

        return new ForwardResolution(VIEW);
    }
}

HelloActionBean is executed when we click on the Submit button. The request parameter is automatically bound to its userName property. The default handler forwards to the greet.jsp view.

@Validate(required=true)
private String userName;

The @Validate annotation is used to enforce validation for the user name field of the form. An error message is displayed if no value is entered.

Validation error message
Figure: Validation error message

The second view in our application is greet.jsp.

greet.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%>
<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Greeting</title>
    </head>
    <body>
        <h3>Hello ${actionBean.userName}</h3>
    </body>
</html>

The greet.jsp shows a greeting message for the user. With the ${actionBean.userName} expression we get the name of the user.

Greeting
Figure: Greeting

The application responds with a simple message.

In this tutorial, we have created two simple web applications with the Stripes web framework. You might be also interested in ZetCode's Java tutorial, Validation filter tutorial, Introduction to Play framework, Introduction to Spark Java, Stripes, MyBatis & Derby tutorial, or EJB tutorial.