This section provides information on the following tags:
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.
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.
The following sections outline the functionality related to the tags demonstrated in this listing.
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.
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 DynaForms is covered in Chapter 17.
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.
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.
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.
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.
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" />
<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.