- Hello World! Application Requirements
- Applying the Model-View-Controller Pattern
- The View Component: The HTML Form and the Form Bean
- MessageResources and Application.properties Files
- The Struts Form Bean: HelloForm.java
- Data Validation and ActionErrors
- The Controller Component: HelloAction.java
- The Model Component (HelloModel.java)
- Passing Data to the View Using Attributes: Constants.java
- Tying It All Together: The struts-config.xml File
- Conclusions
The Struts Form Bean: HelloForm.java
When a user clicks the Submit button on an HTML form built with Struts, the data from that form is populated into a Java bean called a form bean.
A form bean has properties that match up with all the fields on the form. When the form is submitted, the bean properties are automatically populated. In addition, form beans provide support for automatic data validation and resetting of the bean property values. Listing 3.3 presents the file HelloForm.java, it's the form bean that's used to process the form data in our hello.jsp file.
Listing 3.3 The Struts Form Bean for Hello World! (HelloForm.java)
package ch03.hello; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * Form bean for Chapter 03 sample application, "Hello World!" * * @author Kevin Bedell and James Turner */ public final class HelloForm extends ActionForm { // --------------------------------------------------- Instance Variables /** * The person we want to say "Hello!" to */ private String person = null; // ----------------------------------------------------------- Properties /** * Return the person to say "Hello!" to * * @return String person the person to say "Hello!" to */ public String getPerson() { return (this.person); } /** * Set the person. * * @param person The person to say "Hello!" to */ public void setPerson(String person) { this.person = person; } // --------------------------------------------------------- Public Methods /** * Reset all properties to their default values. * * @param mapping The mapping used to select this instance * @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { this.person = null; } /** * Validate the properties posted in this request. If validation errors are * found, return an <code>ActionErrors</code> object containing the errors. * If no validation errors occur, return <code>null</code> or an empty * <code>ActionErrors</code> object. * * @param mapping The current mapping (from struts-config.xml) * @param request The servlet request object */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((person == null) || (person.length() < 1)) errors.add("person", new ActionError("ch03.hello.no.person.error")); return errors; } }
As you can see, a standard Struts form bean is nothing but a simple Java bean with methods added for input validation and to reset the properties to default values. (The two methods (validate() and reset() are actually not required; they override empty stubs in the base ActionForm class.) Struts DynaFormBeans are different and are covered later.
NOTE
The statement: "...must provide bean properties (set/get methods) for each field on the form" is not strictly true. There are some situations where this is not possible.
If an HTML form presents a table with a variable number of rows, for example, the number of fields will vary depending on the number of rows presented. There are also situations where the fields displayed on an HTML form might be completely dynamic. In that case, there might be no way to predict how many fields will be displayed in a form or what their field names might be.
Struts can handle these cases by allowing for the posting of arrays of values or by using DynaForms. A discussion of these components is beyond the scope of this chapter and will be presented in Chapter 17, "DynaForms and the Validator."