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.
...
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 |
---|
xmllanguage | xml | 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="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 |
---|
|
<stripes:form beanclass="net.sourceforge.stripes.examples.quickstart.CalculatorActionBean" focus=""> |
...
Next, we see two tags, something like:
Code Block |
---|
|
<stripes:text name="numberOne"/> |
...
Instead of a <input type="submit">
we see:
Code Block |
---|
|
<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 |
---|
|
<td>${actionBean.result}</td> |
...
Info |
---|
title | Calculator 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 |
---|
language | java |
---|
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");
}
}
|
...
Next, the class declaration looks like
Code Block |
---|
|
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 |
---|
|
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");
}
|
...
Info |
---|
title | Now 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.
...
Info |
---|
title | Validation in Action(Beans)! |
---|
|
Image RemovedImage Added |
Adding (or should I say Dividing) another Operation
...