Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction

This guide is designed to get you up and running with Stripes as quickly as possible. It contains a section on configuring Stripes in a web application, and another on developing your first Stripes application.

...

Code Block
languagexml
titleweb.xml
collapsetrue
 
<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
    version="2.4"> 

    <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>net.            <param-value>net.sourceforge.stripes.examples</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-        <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> 
</web-app> 

...

No Format
titlecommons-logging.properties
  org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
No Format
titlelog4j.properties
  ### direct log messages to stdout ### 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 

### direct messages to file ### 
log4j.appender.file=org.apache.log4j.FileAppender 
log4j.appender.file.File=/tmp/stripes.log 
log4j.appender.file.layout=org.apache.log4j.PatternLayout 
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 

### set log levels - for more verbose logging change 'info' to 'debug' ### 
log4j.rootLogger=INFO, stdout, file 
log4j.logger.net.sourceforge.stripes=DEBUG 

...

No Format
titleExample StripesResources.properties
 
# Validation error messages used by Stripes' built in type converters 
converter.number.invalidNumber=The value ({1}) entered in field {0} must be a valid number 
converter.byte.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} 
converter.short.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} 
converter.integer.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} 
converter.float.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} 
converter.enum.notAnEnumeratedValue=The value "{1}" is not a valid value for field {0} 
converter.date.invalidDate=The value ({1}) entered in field {0} must be a valid date 
converter.email.invalidEmail=The value ({1}) entered is not a valid email address 
... 

...

As a first application we'll develop a simple, one page calculator that can take two numbers and perform additions, and maybe some other operations later. First off, lets get the JSP into shape. The following is a first cut at a JSP. You'll want to put it in a directory called 'quickstart' off your web application root.

The JSP

Code Block
xml
languagexml
title"index.jsp"
 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head><title>My First Stripe</title></head> 
<body> 
<h1>Stripes Calculator</h1> 

Hi, I'm the Stripes Calculator. I can only do addition. Maybe, some day, a nice programmer 
will come along and teach me how to do other things? 

<stripes:form beanclass="net.sourceforge.stripes.examples.quickstart.CalculatorActionBean" focus=""> 
    <table> 
        <tr> 
            <td>Number 1:</td> 
 <td><stripes:text name="numberOne"/></td>    </tr>  <tr>  <td>Number 2:</td>  <td><stripes:text name="numberTwonumberOne"/></td> 
        </tr> 
<tr>   <td colspan="2"> 
<stripes:submit name="addition" value="Add"/> 
</td> 
</tr> 
<tr>     <tr> 
            <td>Number 2:</td> 
            <td><stripes:text name="numberTwo"/></td> 
        </tr> 
        <tr> 
            <td colspan="2"> 
                <stripes:submit name="addition" value="Add"/> 
            </td> 
        </tr> 
        <tr> 
            <td>Result:</td> 
            <td>${actionBean.result}</td> 
        </tr> 
    </table> 
</stripes:form> 
</body> 
</html> 

The first interesting thing in the page above is the second line (the one beginning <%@ taglib .... That imports the Stripes Tag Library for use within the page. Then, a little lower, the line:

Code Block
xml
languagexml
<stripes:form beanclass="net.sourceforge.stripes.examples.quickstart.CalculatorActionBean" focus="">

...

Next, we see two tags, something like:

Code Block
xml
languagexml
<stripes:text name="numberOne"/>

...

Instead of a <input type="submit"> we see:

Code Block
xml
languagexml
<stripes:submit name="addition" value="Add"/>

...

Lastly, a small EL expression is used to print the result property of the ActionBean if it is present.

Code Block
xml
languagexml
<td>${actionBean.result}</td>

...

Info
titleCalculator example page after writing the just the JSP

Image RemovedImage Added

The ActionBean

...

It should be mentioned at this point that there is no need for any external configuration to let Stripes know about the ActionBean implementations in an application, nor to tie together the JSP page and ActionBean. All of the information necessary is in the ActionBean itself. Let's take a look at the simple ActionBean that receives the Calculator's request.

Code Block
languagejava
titleCalculatorActionBean.java
 
package net.sourceforge.stripes.examples.quickstart; 

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; 

/** 
* A very simple calculator action. 
* @author Tim Fennell 
*/ 
public class CalculatorActionBean implements ActionBean { 
    private ActionBeanContext context; 
    private double numberOne; 
    private double numberTwo; 
    private double result; 

    public ActionBeanContext getContext() { return context; } 
    public void setContext(ActionBeanContext context) { this.context = context; } 

    public double getNumberOne() { return numberOne; } 
    public void setNumberOne(double numberOne) { this.numberOne = numberOne; } 

    public double getNumberTwo() { return numberTwo; } 
    public void setNumberTwo(double numberTwo) { this.numberTwo = numberTwo; } 

    public double getResult() { return result; } 
    public void setResult(double result) { this.result = result; }

    @DefaultHandler 
    public Resolution addition() { 
        result = getNumberOne() + getNumberTwo(); 
        return new ForwardResolution("/quickstart/index.jsp"); 
    } 
} 

Notice that we used the ActionBean's class name in the stripes:form tag. Nevertheless, it's good to know how Stripes goes from the class name to a URL. If you look at the generated code, you'll see action="/examples/quickstart/Calculator.action in the HTML. By default Stripes will examine ActionBeans and determine their URL based on their class and package names. To convert class names to URLs Stripes:

...

Next, the class declaration looks like

Code Blockcode
languagejava
public class CalculatorActionBean implements ActionBean

ActionBean (if you hadn't gathered by this point) is an interface, not a base class. As a result, your ActionBeans may extend any class you like. The ActionBean interface defines two methods, which we see implemented in the class as:

Code Block
languagejava
 
public ActionBeanContext getContext() { return context; } 
public void setContext(ActionBeanContext context) { this.context = context; } 

...

Then the really interesting bit.

Code Block
languagejava
@DefaultHandler 
public Resolution addition() { 
    result = numberOne + numberTwo; 
    return new ForwardResolution("/quickstart/index.jsp"); 
} 

...

Info
titleNow the ActionBean is written, we can add two plus two

Image RemovedImage Added

That simple JSP, and short ActionBean class are all that's needed to put together a working example in Stripes. But for extra credit we can do more.

...

Code Block
titleAdding Required Field Validation
 
@Validate(required=true) private double numberOne; 
@Validate(required=true) private double numberTwo; 

...

Code Block
xml
xml
titleShowing Validation Errors
 
<stripes:form beanclass="net.sourceforge.stripes.examples.quickstart.CalculatorActionBean">
<stripes:errors/> 

The <stripes:errors/> tag will output any validation errors for the form if there are any present. It's presentation is customizable, but we won't go into that here. In addition, any form field that is in error will have it's css class attribute changed to error. So if we were to add the following to the HTML head, fields in error would get yellow backgrounds.

Code Block
xml
xml
 
<style type="text/css"> 
    input.error { background-color: yellow; } 
</style> 

...

Info
titleValidation in Action(Beans)!

Image RemovedImage Added

Adding (or should I say Dividing) another Operation

This is great and all, but what if we wanted to build out some other operations? Say division? Well, it turns out that's pretty straightforward. First we make a quick addition to the JSP:

Code Block
xml
xml
 
<td colspan="2"> 
    <stripes:submit name="addition" value="Add"/> 
    <stripes:submit name="division" value="Divide"/> 
</td> 

Next we add a new Handler method to the ActionBean:

Code Block
 
public Resolution division() { 
    result = numberOne / numberTwo; 
    return new ForwardResolution("/quickstart/index.jsp"); 
} 

The validations we defined earlier apply when the "division" event is fired, just like when the "addition" event is fired. However, we should probably ensure that when a division event occurs that the user isn't trying to trick the system into dividing by zero. We could write the validation code in the division() method if we wanted, but instead we'll add a ValidationMethod:

Code Block
 
@ValidationMethod(on="division") 
public void avoidDivideByZero(ValidationErrors errors) { 
    if (this.numberTwo == 0) { 
        errors.add("numberTwo", new SimpleError("Dividing by zero is not allowed.")); 
    } 
} 

Methods which perform validation are marked with a @ValidationMethod annotation to tell Stripes to run them prior to executing the Handler method. Unless otherwise specified Stripes will execute validation methods for all events; in this case we've restricted the method to be run only on division events. The method is passed a reference to the ValidationErrors object which is used to store validation errors for the current event.

...