...
Code Block |
---|
language | xml |
---|
title | web.xml |
---|
collapse | true |
---|
|
<?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 |
---|
title | commons-logging.properties |
---|
|
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
|
No Format |
---|
|
### 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 |
---|
title | Example 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
...
|
...
Code Block |
---|
|
<%@ 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"> <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>/td>
</tr>
</table>
</stripes:form>
</body>
</html>
|
...
Code Block |
---|
title | CalculatorActionBean.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:
...
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 |
---|
public ActionBeanContext getContext() { return context; }
public void setContext(ActionBeanContext context) { this.context = context; }
|
...
Then the really interesting bit.
Code Block |
---|
@DefaultHandler
public Resolution addition() {
result = numberOne + numberTwo;
return new ForwardResolution("/quickstart/index.jsp");
}
|
...
Code Block |
---|
title | Adding Required Field Validation |
---|
|
@Validate(required=true) private double numberOne;
@Validate(required=true) private double numberTwo;
|
...
Code Block |
---|
| xml |
---|
| xml |
---|
title | Showing 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 |
---|
|
<style type="text/css">
input.error { background-color: yellow; }
</style>
|
...
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 |
---|
|
<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.
...