JavaBeat
Stripes Home Articles Tutorials Java Interview Questions & FAQs Java QnA Code Junction

1. Stripes WebFramework- Introduction
2. Requirements
3. Configuring Stripes
4. My First Stripe
5. The ActionBean
6. Adding Validation
7. Adding another Operation

The ActionBean

An ActionBean is the 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. To compare to Struts, the ActionBean is like the ActionForm and the Action put together in one class.

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.

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");
}
}

The obvious question here is, how does this class get bound to a URL? By default Stripes will examine ActionBeans and determine their URL based on their class and package names. To convert class names to URLs Stripes:

  • Removes any package names up to and including packages called 'web', 'www', 'stripes' and 'action'
  • Removes 'Action' and 'Bean' (or 'ActionBean') if it is the last part of the class name
  • Converts it to a path and appends '.action'

So in the above case, net.sourceforge.stripes.examples.quickstart.CalculatorActionBean became:

  • examples.quickstart.CalculatorActionBean
  • examples.quickstart.Calculator
  • /examples/quickstart/Calculator.action

The URL generated from the class name matches the action specified in the stripes:form tag on the JSP. In both cases this is relative to the web application root. You can read more about how this is handled (and how to change the conversion to create different URLs) in the JavaDoc for NameBasedActionResolver.



Next, the class declaration looks like

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:

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

These methods provide the ActionBean with access to the ActionBeanContext which provides access to the HttpServletRequest and HttpServletResponse for when you need them (hopefully not often), as well as other information about the current request (e.g. error messages).

While the individual getters and setters for the properties defined on the ActionBean are not really that interesting, it should be noted that they are necessary (unless properties are public, but that's discouraged). Stripes accesses values on ActionBeans through standard JavaBean getter and setter methods, and if they do not exist you will get errors. The three properties (and hence getter/setter pairs) on the ActionBean correspond to the names used on the JSP.

Then the really interesting bit.

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

Because this method is public and returns a Resolution Stripes will identify it as a handler method. When a request comes to the CalculatorActionBean, and the user hit a submit button or image button with the name (name not value) "addition", that this method will be invoked. Just like with the URL above, the name of the event that a method handles can be overridden using the @HandlesEvent annotation.

The @DefaultHandler annotation tells Stripes that if it cannot determine what button the user hit (often because the user hit enter instead of clicking a button) that this method should be invoked.

You might have realized at this point that there is no execute() or do() or other generic sounding method in the ActionBean interface. All the execute() style methods are regular Java methods that have a recognizable signature (public, non-abstract, return Resolution) or have been annotated to let Stripes know about them. Stripes refers to these as Handler methods, since they handle events from the browser. Handler methods usually return a Resolution, which tell Stripes what to do next (they may return anything, but if it is not a Resolution, Stripes will ignore it).

Our method adds the two numbers together and stores the sum in the result property and then forwards to the same JSP we came from by returning a ForwardResolution. That's it!

Now the ActionBean is written, we can add two plus two

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.


Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat