Using the Struts Framework to Develop a Message Board--Part 3: Developing the View Components for the Message Board
- Using the Struts Framework to Develop a Message Board--Part 3: Developing the View Components for the Message Board
- Create the Output JSP That Displays a Message
- Creating a Help Page
- Creating the Localized Resource Bundles
The view components comprise the JSPs for the input forms and output pages. To internationalize text for the pages, you will also need to add entries for each internationalized resource in the properties file.
Create the Input JSP with the Form
The next step is to create the JSP containing the HTML form. Instead of using the HTML form elements, we will use the custom tag equivalents provided in the Struts framework, as shown in Listing 1. Using the custom tags is advantageous because the framework allows you to initialize the input fields with data using bean introspection that matches the ActionForm properties with form elements to equivalent names. The form tag's action attribute specifies a servlet-mapping action name: add.do. Part 4 of this series, "Developing the Controller for the Application" explains this more thoroughly. Part 4 also specifies the model class: Message, which is used to populate the form elements and the name of the variable in the session scope messageForm.
In addition to the Struts' form tags, the errors custom tag allows you to display the errors you encountered while validating the form.
To internationalize the text, such as the title in the output page, we will use the message tag that accepts the key of the actual message. The actual message is stored in the same properties file that contains the error messages.
Listing 1 message.jsp
<%@ taglib uri="/WEB-INF/struts.tld" prefix="struts" %> <html> <head> <title><struts:message key="message.title"/></title> </head> <body> <struts:errors/> <struts:form action="add.do" name="messageForm" focus="subject" type="Message"> <struts:hidden property="parentId"/> <table width="400"> <tr> <td><struts:message key="message.name"/></td> <td><struts:text property="name" size="20"/></td> <tr></tr> <td><struts:message key="message.email"/></td> <td><struts:text property="email" size="20"/></td> <tr></tr> <td><struts:message key="message.subject"/></td> <td><struts:text property="subject" size="20"/></td> <tr></tr> <td valign="top"><struts:message key="message.body"/></td> <td><struts:textarea property="body" rows="4"/></td> </table> <struts:submit/> <struts:reset/> </struts:form> </body> </html>