Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

The View Component: The HTML Form and the Form Bean

For this application, the best place to start is with the View component. The View in this case is made up of a JSP file and a Struts form bean.

Figure 3.1 shows the HTML form for this application. This is the only HTML page in the application.

Figure 3.1Figure 3.1 The Hello World! application.

Listing 3.1 shows the JSP file used to create this HTML page. (This book assumes that you have a basic understanding of Java and JSP, so the discussion around Listing 3.1 will focus on the Struts portions of the file.)

Listing 3.1 The JSP File for the Hello World! Application (hello.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html locale="true">
  <title><bean:message key="hello.jsp.title"/></title>
 <body bgcolor="white"><p>

  <h2><bean:message key="hello.jsp.page.heading"/></h2><p>


  <logic:present name="ch03.hello" scope="request">
     Hello <bean:write name="ch03.hello" property="person" />!<p>

  <html:form action="/HelloWorld.do?action=gotName" focus="username" >

   <bean:message key="hello.jsp.prompt.person"/>
   <html:text property="person" size="16" maxlength="16"/><br>

   <html:submit property="submit" value="Submit"/>


  <html:img page="/struts-power.gif" alt="Powered by Struts"/>


The first thing you notice in the JSP file are the Struts custom tags; for example, <html:form> and <bean:message>. The Struts custom tags provide features that are either unavailable in standard JSP or that help tie the JSP page into the rest of the Struts framework. The Struts tags are built using standard JSP custom tag functionality. (More information about JSP custom tags will be provided in Chapter 5, " JSP, Taglibs, and JSTL: Extending Java onto the Page.")

The second thing you might notice is that none of the text on the HTML page is actually in the JSP file. Instead, there are property names such as hello.jsp.title. This is how Struts handles localization and internationalization (referred to as i18n because there are 18 letters between the i and n in the word internationalization) of your application.

For now, just understand that the text you see on this HTML page is put there by the Struts <bean:message> tags. For example, the line

<bean:message key="hello.jsp.prompt.person"/>

prints the prompt in the form. The key values for these tags are defined in one or more property files. There is one properties file for each locale for which your application needs to present localized text. More on this follows in the next section.

Now let's get back to the Struts tags. The most important function they provide is to tie the Struts View components to the rest of the framework. They enable you to access and present the data passed in from the Controller and Model components. Details on all the tags will be provided in other chapters, but we'll look at some of the more important ones here.

To begin with, the lines

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

identify and load the Struts tag library definitions. These lines indicate we are using the Struts bean, html, and logic tag libraries. This is standard JSP syntax to load the tag libraries and make the tags available for use in the file.

There are a number of HTML tags in this file, including <html:errors>, <html:form>, and <html:text>. Let's look at these three:

  • <html:errors>—This tag is used to access and present the results of Struts' data validation. Errors detected by other portions of the framework are accessible to the View component via this tag.

  • <html:form>—This tag is used for all HTML form processing in Struts; it ties the form fields to properties in Struts form beans. It also ties the fields into Struts' automatic form validation. Form beans (to be covered shortly) are Java beans that transfer the values entered in a form to the Controller component (that is, the Struts Action class). Each form field will be tied to a corresponding property in the form bean.

  • <html:text>—This tag is used inside an <html:form> tag. It ties a text field in the form to a property in the form bean. In this case, the line

    <html:text property="person" size="16" maxlength="16"/>

    ties this form field to the person property in the form bean. Upon submission of the form, the data in the field (or null if the field is empty) will be stored in the form bean using the setPerson() method.


The description of how the person field is populated into the form bean is valid only for a normal Struts form bean. A second type of form bean, called a DynaFormBean, handles data posting differently. This will be covered in more detail in Chapter 17, "DynaForms and the Validator."

There are two Struts bean tags in the file—the <bean:message> and <bean:write> tags:

  • <bean:message>—This tag is used to output locale-specific text from a MessageResources bundle.

  • <bean:write>—This is a general-purpose tag that's used to output property values from a bean. <bean:write> is a commonly used tag that provides a great deal of power and flexibility in presenting data. Here, the tag writes the value of the person property of the bean named ch03.hello to the JSP output.

Finally, our first application uses a single logic tag:

  • <logic:present>—This tag renders its output only if a bean is present and available to the JSP page. It is one of a number of tags that allow for conditional presentation based on logic or substring pattern matching. Its opposite tag is <logic:notPresent>. This tag renders the output inside it only if a bean named ch03.hello is present and available in the request scope.

  • + Share This
  • 🔖 Save To Your Account