本例中,视图包括两个组件:
    ·一个JSP文件:hello.jsp
    ·一个ActionForm Bean: HelloForm Bean
    下面分别讲述如何创建这两个组件。
     创建JSP文件
    hello.jsp提供用户界面,能够接受用户输入的姓名。此外,本Web应用的所有输出结果也都由hello.jsp显示给用户。图2-1显示了hello.jsp提供的网页。
362125.jpg
图2-1 hello.jsp的网页
    在图2-1中,用户输入姓名"Weiqin"后,按提交表单,本应用将返回"Hello Weiqin!",参见图2-2。
362127.jpg
图2-2 hello.jsp接受用户输入后正常返回的网页
    例程2-1为hello.jsp文件的源代码。
例程2-1  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="personbean" scope="request">
       <h2>
         <bean:message key="hello.jsp.page.hello"/>
         <bean:write name="personbean" property="userName" />!<p>
       </h2>
    </logic:present>
    <html:form action="/HelloWorld.do" focus="userName" >
      <bean:message key="hello.jsp.prompt.person"/>
      <html:text property="userName" 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>
    以上基于Struts框架的JSP文件有以下特点:
    ·没有任何Java程序代码
    ·使用了许多Struts的客户化标签,例如<html:form>和<logic:present>标签
    没有直接提供文本内容,取而代之的是<bean:message>标签,输出到网页上的文本内容都是由<bean:message> 标签来生成的。例如:
<bean:message key="hello.jsp.prompt.person"/>
    Struts客户化标签是联系视图组件和Struts框架中其它组件的纽带。这些标签可以访问或显示来自于控制器和模型组件的数据。在本书第12章至16章讲专门介绍Struts标签的用法,本节先简单介绍几种重要的Struts标签。
    hello.jsp开头几行用于声明和加载Struts标签库:
<%@ 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" %>
    以上代码表明该JSP文件使用了Struts Bean、Html和Logic 标签库,这是加载客户化标签库的标准JSP语法。
    hello.jsp中使用了来自 Struts HTML标签库中的标签,包括<html:errors>, <html:form>和<html:text>:
    ·<html:errors>:用于显示Struts框架中其他组件产生的错误消息。
    ·<html:form>:用于创建HTML表单,它能够把HTML表单的字段和ActionForm Bean的属性关联起来。
    ·<html:text>:该标签是<html:form>的子标签,用于创建HTML表单的文本框。它和ActionForm Bean的属性相关联。
    hello.jsp中使用了来自Struts Bean标签库的两个标签<bean:message>和<bean:write>:
    <bean:message>:用于输出本地化的文本内容,它的key属性指定消息key,和消息key匹配的文本内容来自于专门的Resource Bundle,关于Resource Bundle的概念参见本书第9章(Struts应用的国际化)。
    <bean:write>:用于输出JavaBean的属性值。本例中,它用于输出personbean对象的userName属性值:
<bean:write name="personbean" property="userName" />
    hello.jsp使用了来自Struts Logic标签库的<logic:present>标签。<logic:present>标签用来判断JavaBean在特定的范围内是否存在,只有当JavaBean存在,才会执行标签主体中的内容:
<logic:present name="personbean" scope="request">
       <h2>
         Hello <bean:write name="personbean" property="userName" />!<p>
       </h2>
</logic:present>
    本例中,<logic:present>标签用来判断在request范围内是否存在personbean对象,如果存在,就输出personbean的userName属性值。和<logic:present>标签相对的是<logic:notPresent>标签,它表示只有当JavaBean在特定的范围内不存在,才会执行标签主体中的内容。
     创建消息资源文件
    hello.jsp使用<bean:message>标签来输出文本内容。这些文本来自于Resource Bundle,每个Resource Bundle都对应一个或多个本地化的消息资源文件,本例中的资源文件为application.properties,例程2-2是该消息资源文件的内容。
例程  2-2 application.properties文件
#Application Resources for the "Hello" sample application
hello.jsp.title=Hello - A first Struts program
hello.jsp.page.heading=Hello World! A first Struts application
hello.jsp.prompt.person=Please enter a UserName to say hello to :
hello.jsp.page.hello=Hello
#Validation and error messages for HelloForm.java and HelloAction.java
hello.dont.talk.to.monster=We don't want to say hello to Monster!!!
hello.no.username.error=Please enter a <i>UserName</i> to say hello to!
    以上文件以"消息key/消息文本"的格式存放数据,文件中"#"后面为注释行。对于以下JSP代码:
<bean:message key="hello.jsp.title"/>
<bean:message>标签的key属性为"hello.jsp.tilte",在Resource Bundle中与之匹配的内容为:hello.jsp.title=Hello - A first Struts program
    因此,以上<bean:message>标签将把"Hello - A first Struts program"输出到网页上。
     创建ActionForm Bean
    当用户提交了HTML表单,Struts框架自动把表单数据组装到ActionForm Bean中。ActionForm Bean中的属性和HTML表单中的字段一一对应。ActionForm Bean还提供数据验证方法,以及把属性重新设置为默认值的方法。Struts框架中定义的ActionForm类是抽象的,必须在应用中创建它的子类,来存放具体的HTML表单数据。例程2-3为HelloForm.java的源程序, 它用于处理hello.jsp中的表单数据。
例程2-3  HelloForm.java
package hello;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public final class HelloForm extends ActionForm {
    private String userName = null;
    public String getUserName() {
        return (this.userName);
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
   
    /**
     * Reset all properties to their default values.
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.userName = null;
    }
    /**
     * Validate the properties posted in this request. If validation errors are
     * found, return an <code>ActionErrors</code> object containing the errors.
     * If no validation errors occur, return <code>null</code> or an empty
     * <code>ActionErrors</code> object.
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if ((userName == null) || (userName.length() < 1))
            errors.add("username", new ActionMessage("hello.no.username.error"));
        return errors;
    }
}
    从以上代码可以看出,ActionForm Bean实质上是一种JavaBean,不过它除了具有JavaBean的常规方法,还有两个特殊方法:
    ·validate():用于表单验证。
    ·reset():把属性重新设置为默认值。(T111)