JavaServer Pages: Generating Dynamic Content
Pages: 1, 2, 3, 4, 5, 6, 7, 8
Keep On Doing It 'til You Get It Right
Okay, now you know how to set bean properties and you're aware that beans often validate their values. It would be nice if this technique could be used to display the same form over and over until all required input is correct. You can do that with just a few changes, as shown in Example 5-4, the userinfo2.jsp page.
Example 5-4: A JSP Page that Validates and Redisplays Until Correct (userinfo2.jsp)
<%@ page language="java" contentType="text/html" %><html><head><title>User Info Entry Form</title></head><body bgcolor="white"><jsp:useBeanid="userInfo"class="com.ora.jsp.beans.userinfo.UserInfoBean"><jsp:setProperty name="userInfo" property="*" /></jsp:useBean><%-- Output list of values with invalid format, if any --%><font color="red"><jsp:getProperty name="userInfo" property="propertyStatusMsg" /></font><%-- Output form with submitted valid values --%><form action="userinfo2.jsp" method="post"><table><tr><td>Name:</td><td><input type="text" name="userName"value="<jsp:getPropertyname="userInfo"property="userName"/>"></td></tr><tr><td>Birth Date:</td><td><input type="text" name="birthDate"value="<jsp:getPropertyname="userInfo"property="birthDate"/>"></td><td>(Use format yyyy-mm-dd)</td></tr><tr><td>Email Address:</td><td><input type="text" name="emailAddr"value="<jsp:getPropertyname="userInfo"property="emailAddr"/>"></td><td>(Use format name@company.com)</td></tr><tr><td>Sex:</td><td><input type="text" name="sex"value="<jsp:getPropertyname="userInfo"property="sex"/>"></td><td>(Male or female)</td></tr><tr><td>Lucky number:</td><td><input type="text" name="luckyNumber"value="<jsp:getPropertyname="userInfo"property="luckyNumber"/>"></td><td>(A number between 1 and 100)</td></tr><tr><td colspan=2><input type="submit"></td></tr></table></form></body></html>
Instead of using a static HTML page for the input form and a separate JSP page with the validation code, in this example we have combined them into a single JSP page. This page generates the form and provides an appropriate message based on whether or not the input is valid. The page also fills in the form with the valid values that have already been specified (if any) so the user needs to enter values only for missing or incorrect input.
Let's look at Example
5-4 from the top. The first thing to note is that the page generates a
message using the UserInfoBean property named propertyStatusMsg. Here is the corresponding snippet:
<%-- Output list of values with invalid format, if any --%><font color="red"><jsp:getProperty name="userInfo" property="propertyStatusMsg" /></font>
The first line here is a JSP comment. Text between <%-- and --%> in a JSP
page is treated as a comment and never appears in the result sent to the
browser. For complex pages, it's always a good idea to include comments to
explain things that are not obvious.
The propertyStatusMsg property can
have three different values. If none of the properties have been set, the
value is "Please enter values in all fields". If at least one value is missing
or invalid, the message states "The following values are missing or invalid"
and provides a list of the relevant properties. Finally, if all the values are
valid, the propertyStatusMsg is "Thanks for telling
us about yourself!"
Next we generate the form, filled out with all valid values.
Here's the beginning of the form and the code for the userName property:
<%-- Output form with submitted valid values --%><form action="userinfo2.jsp" method="post"><table><tr><td>Name:</td><td><input type="text" name="userName"value="<jsp:getPropertyname="userInfo"property="userName"/>"></td></tr>
Most of this is plain HTML, which is treated as template text
and passed on untouched to the browser. But note the use of a <jsp:getProperty> action as the HTML <input> element's value
attribute. This is how the userName field in the
form is filled in with the current value of the userName bean property. Also note how the form's action attribute points back to the JSP page itself.
Try this out by clicking on the "User Info 2 example" link on the book examples page. Enter both valid and invalid values in the form and look at the results. In Chapter 8, we'll expand on this example and look at how you can move on to another page when all input is valid.
One item may look a bit strange to you: an element (<jsp:getProperty>) is used as the value of another
element's attribute (the <input> tag's value attribute). While this is not valid HTML syntax, it
is valid JSP syntax. Remember that everything not
recognized as a JSP element is treated as template text. Whether the template
text is HTML, XML, WML, or just plain text doesn't matter. As far as the JSP
container is concerned, the previous code is as valid as:
any old template text <jsp:getPropertyname="userInfo"property="userName" /> more text
When the JSP page is processed, the action element is replaced with the value of the bean's property. The resulting HTML sent to the browser is therefore valid.