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.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"> <head> <title><bean:message key="hello.jsp.title"/></title> <html:base/> </head> <body bgcolor="white"><p> <h2><bean:message key="hello.jsp.page.heading"/></h2><p> <html:errors/><p> <logic:present name="ch03.hello" scope="request"> <h2> Hello <bean:write name="ch03.hello" property="person" />!<p> </h2> </logic:present> <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:reset/> </html:form><br> <html:img page="/struts-power.gif" alt="Powered by Struts"/> </body> </html:html> 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:
Note 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:
Finally, our first application uses a single logic tag:
|