The Basics of Form Processing


This section provides information on the following tags:

  • <html:form> ” Render an HTML <form> element

  • <html:text> ” Place a text box INPUT element on a form

  • <html:hidden> ” Place a INPUT type=hidden element on a form

  • <html:submit> ” Place a Submit INPUT element on a form

  • <html:cancel> ” Place a Cancel INPUT element on a form

  • <html:reset> ” Place a Reset INPUT element on a form

If you point your browser to the URL

 http://myAppServer/StrutsTaglibs/html.jsp 

you'll bring up the main page that links to all the sample code for the Struts tag chapters. This section uses the Form Basics page at http://localhost:8080/StrutsTaglibs/FormBasics.do . The rendered page is shown in Figure 12.3.

Figure 12.3. The Form Basics page at /StrutsTaglibs/FormBasics.do .

graphics/12fig03.gif

As you can see, Figure 12.3 contains a very basic form along with some hidden variables and Submit, Cancel, and Reset buttons . Listing 12.2 is the JSP file that creates this page.

Listing 12.2 JSP File Demonstrating the Basic Struts HTML Tags for Form Processing ( FormBasics.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html:html> <head> <title>Form Basics sample code</title> </head> <body bgcolor="white"> <h3>Form Basics sample code</h3> <p>This page provides examples of the following Struts HTML tags:<br> <ul> <li>&lt;html:form&gt;</li> <li>&lt;html:text&gt;</li> <li>&lt;html:hidden&gt;</li> <li>&lt;html:submit&gt;</li> <li>&lt;html:cancel&gt;</li> <li>&lt;html:reset&gt;</li> </ul> <html:form action="FormBasics.do"> <table border="1" width="100%">   <tr>     <th colspan="3" align="left">       Sample &lt;html:hidden&gt; tags     </th>   </tr>   <tr>     <td align="left" >       Enter value to become hidden: <html:text property="hiddenValue"/>     </td>     <td align="left" >       &lt;html:hidden&gt; tags are rendered below     </td>   </tr>   <tr>     <td align="left" >       &nbsp; &lt;html:hidden property="hiddenValue" /&gt;     </td>     <td align="left" >       &nbsp; <html:hidden property="hiddenValue" />     </td>   </tr>   <tr>     <td align="left" >       &nbsp; &lt;html:hidden property="hiddenValue" write="true" /&gt;     </td>     <td align="left" >       &nbsp; <html:hidden property="hiddenValue" write="true" />     </td>   </tr> </table> <br> <table border="1" width="100%">   <tr>     <th colspan="3" align="left">       Page Cancel status     </th>   </tr>   <tr>     <td align="right">       Which was pressed?:     </td>     <td align="left">       <html:text property="status" disabled="true" />     </td>   </tr> </table> <table border="0" width="100%">   <tr>     <td align="right">       Press Submit, Cancel or Reset:     </td>     <td align="left">       <html:submit>Submit</html:submit>       <html:cancel>Cancel</html:cancel>       <html:reset>Reset</html:reset>     </td>   </tr> </table> </html:form> </body> </html:html> 

The following sections outline the functionality related to the tags demonstrated in this listing.

Form Basics: The <html:form> Tag

Form processing is at the heart of most Web-based applications. In Struts, every form processed requires an <html:form> at its beginning and an </html:form> tag at its end.

For example, in the sample code, here's the <html:form> tag we used:

 <html:form action="FormBasics.do"> 

This generates the HTML <form> tag:

 <form name="FormBasicsForm" method="POST"                             action="/StrutsTaglibs/FormBasics.do"> 

This is the most basic type of <html:form> tag. It simply specifies which <action> section of the struts-config.xml is used to determine how the form will be processed. Here's the corresponding struts-config.xml entry for this <html:form> tag:

 <!-- Form Basics example code -->     <action  path="/FormBasics"              type="ch12.FormBasicsAction"              name="FormBasicsForm"              scope="session"              validate="false"     >       <forward name="default" path="/FormBasics.jsp"/>     </action> 

Because only the action attribute of the <html:form> tag is specified in this example, Struts takes defaults for the form processing from this <action> entry.

If you need to override these defaults and change how your form processes, you can do so by directly specifying new values for the type , name , and/or scope attributes in the <html:form> tag. For example, we could specify a different form bean using the following example:

 <html:form action="FormBasics.do" name="ch12.diffBean" type="ch12.diffBean"> 

This line enables you to override the struts-config.xml entry and lets you specify the form bean directly in the <html:form> tag itself.

The <html:text> Tag

This tag creates an input text field. It's one of the most-used Struts tags.

There are two examples of the <html:text> tag in this example. The examples are similar in that they both map a text field directly to a property in the form bean. Here's one of the examples:

 <html:text property="hiddenValue"/> 

In this example, hiddenValue is mapped to the "hiddenValue" property of the form bean. When the form is posted, the value in this text field is used to populate the "hiddenValue" property of the form bean. In addition, when the page is created, the value of this form bean property becomes the initial value for the field.

Using the <html:text> tag with Struts DynaForm s is covered in Chapter 17.

The <html:cancel> Tag

The <html:cancel> tag generates a form Cancel button.

Processing the <html:cancel> tag requires both placing the tag in the JSP file, and coding business logic in the Action class to capture the cancel event when it occurs. This is because it's in the Action class that a cancel event most likely must be processed.

When a user cancel event is detected , it's possible that the user has previously clicked submit and then grown tired of waiting for a response. (Okay, I know: Users never do that!) If this is the case, you might appreciate a chance to clean things up rather than leaving things in an unpredictable state. Here's how you do it.

First, place the <html:cancel> tag on the form. In our example, this is simply coded as

 <html:cancel>Cancel</html:cancel> 

This places a Cancel button on the form and generates the following HTML in the page:

 <input type="submit" name="org.apache.struts.taglib.html.CANCEL"                      value="Cancel"> 

You need to place code in the Action class to detect the cancel when it occurs. Listing 12.3 is the Action class for this example.

Listing 12.3 The Action Class for the FormBasics Example; This Demonstrates Handling a Cancel Event ( FormBasicsAction.java )
 package ch12; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /**  * Action class to demonstrate handling an <html:cancel> tag  *  * @author Kevin Bedell & James Turner  * @version 1.0  */ public class FormBasicsAction extends Action {     /**      * Do Nothing except forward the request      *      * @param mapping The ActionMapping from this struts-config.xml entry      * @param actionForm The ActionForm to process, if any      * @param request The JSP request object      * @param response The JSP response object      *      * @exception Exception if business logic throws an exception      */     public ActionForward execute(ActionMapping mapping,                                  ActionForm form,                                  HttpServletRequest request,                                  HttpServletResponse response)         throws Exception {         FormBasicsForm fbf = (FormBasicsForm) form;         if (isCancelled(request)) {             /*              * If request was cancelled, we would clean up any processing              * that was unfinished and release any resources we may              * have locked.              */             // Set status to reflect that cancel WAS pressed!             fbf.setStatus("Cancel was pressed!");             return (mapping.findForward("default"));         }  else {             // Set status to reflect that cancel WAS NOT pressed!             fbf.setStatus("Submit was pressed!");             return (mapping.findForward("default"));         }     } } 

As you can see, the way to detect a cancel is by using the isCancelled(request) method. If this method returns true, you perform any required clean up. You can also forward the page to a particular JSP (View component), as the code demonstrates.

The <html:reset> Tag

This tag generates a form Reset button.

This example places a Reset button on the form. The Struts tag in our example is

 <html:reset>Reset</html:reset> 

The HTML generated is

 <input type="reset" name="reset" value="Reset"> 

This is a basic, no-frills tag and virtually all the attributes for it are simply to pass through HTML attributes.

The <html:submit> Tag

This tag generates a form Submit button.

This example places a Submit button on the form. The Struts tag in our example is

 <html:submit>Submit</html:submit> 

The HTML generated is

 <input type="submit" name="submit" value="Submit"> 

As with the <html:reset> button, this is a basic, no-frills tag and virtually all the attributes for it are simply to pass through HTML attributes.

The <html:hidden> Tag

This tag generates a hidden input element in a form.

Hidden values are commonly used to store information on a form that the user doesn't need to see. The values are hidden from the user, but they become visible if the user chooses to view source on the page. Hidden values are sometimes used to store state information in the client browser without using cookies.

This sample application demonstrates two versions of this tag. Both versions create an HTML <input type="hidden" > tag. The second also echoes the value of the hidden property to allow it to be displayed in the page.

Here's the first version:

 <html:hidden property="hiddenValue" /> 

The HTML created is

 <input type="hidden" name="hiddenValue" value="propValue"> 

The second version adds the write=true attribute:

 <html:hidden property="hiddenValue" write="true" /> 

The HTML created is

 <input type="hidden" name="hiddenValue" value="propValue">propValue 

As you can see, the second version simply echoes the value of the hidden variable. This can be useful if you need to display a value from your form bean and don't want the user to be able to change it.



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net