Software By JeffMain Page | About | Help | FAQ | Special pages | Log in
The Free Encyclopedia
Printable version | Disclaimers

Our Struts Classes

From Software By Jeff

Starting with Struts - Setting Up Struts - Struts Webserver Configuration - Our Struts Classes - Our Struts Web Pages - Struts Server Deployment - Making Struts Projects in IDEs


Now that the WEB-INF/web.xml file and the referenced WEB-INF/struts-config.xml file are complete, we need to get to the work of making the classes referenced by the struts-config.xml.

In our simple example, one Action class and one ActionForm class are needed. Since we're working our way from the middle, we'll prepare our ActionForm to have only one property, and our Action will simply verify that the property is set. Later we'll discuss some of the more useful patterns for handling a variety of properties.

ExampleForm

Our simple ExampleForm class, which must extend from org.apache.struts.action.ActionForm will have one String property named checkbox. We'll have the appropriate setter and getter. The ActionForm bean is intended to be light-weight, as it is the only data passing from the JSP to the Action class, and we generally want to keep our bandwidth low.

package package;
class ExampleForm extends org.apache.struts.action.ActionForm {
  private String checked;
  public void setChecked(String checked){
    this.checked = checked;
  }
  public String getChecked(){
    return checked;
  }
}

Very simple.

Since our declaration said the type of the class was package.ExampleForm, we put the class in that package. Of course, package is a poor package name, and you should choose something appropriate. The ExampleForm.java file would go in our Java source directory, in the appropriate structure for whatever you replaced package with; that is, if you'd said com.mydomain.struts.forms, you'd make a directory com containing a directory mydomain containing a directory struts containing a directory forms that contains our ExampleForm.java file, and you'd change the package definition in our struts-config.xml file to use com.mydomain.struts.forms.ExampleForm and not package.ExampleForm.

ExampleAction

The Action is a little more complex, but not really so complex as to be confusing.

package package;
class ExampleAction extends org.apache.struts.action.Action {

  public ActionForward execute( ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request, 
                                HttpServletResponse response) 
                       throws Exception {

    ExampleForm exampleForm = (ExampleForm)form;
    if( (form == null) || !"on".equalsIgnoreCase(form.getChecked()) ){
        return mapping.findForward("failure");
    }

    return mapping.findForward("success");
  }
}

Again, the package name package is incorrect, and you should change that to make sense.

So what does this class do for us?

Our default execute() method is called when Struts executes the action we've defined with the name /example, so for URLs like http://server/path/example.do we'll come into this class.

Since our action defines a bean (using the name and type attributes of the action), we know that the form parameter we're passed should be our ExampleForm. We simply cast the passed value to our expected type and move on.

Very simply, the method verifies that the bean's checked value equals something like "on," while disregarding case. If the form or value is null or something other than "on," we find the forward defined with the name failure and pass our bean and work to the defined target. If the form is not null, the value not null, and the value equals "on" in some form, we find the forward defined with the name success and pass that on as our target.

Note that we're in the center of our bean's life-cycle here. The bean should have been defined by the JSP initiating the action. We can use the bean in any legitimate way; we can review, create, alter, and remove values as we see fit. Our modified bean (from the form parameter reference) then will be passed on to our target. Since we've got an ultra-simple bean, we're not doing that now. We'll think of a better example later.

That's it. We've now completed the server-side configuration for our Struts example.

Retrieved from "http://www.swbyjeff.com/index.php/Our_Struts_Classes"

This page has been accessed 3541 times. This page was last modified 19:53, 13 Jan 2006.


Find
Browse
Main Page
Community portal
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Create an account or log in
Special pages
New pages
Image list
Statistics
Bug reports
More...