首页 > 开发 > JSP > 正文

创建JSP文件和ActionForm Bean

2020-02-05 13:40:53
字体:
来源:转载
供稿:网友
   本例中,视图包括两个组件:
    ·一个jsp文件:hello.jsp
    ·一个actionform bean: helloform bean
    下面分别讲述如何创建这两个组件。

    创建jsp文件

    hello.jsp提供用户界面,能够接受用户输入的姓名。此外,本web应用的所有输出结果也都由hello.jsp显示给用户。图2-1显示了hello.jsp提供的网页。


图2-1 hello.jsp的网页


    在图2-1中,用户输入姓名"weiqin"后,按提交表单,本应用将返回"hello weiqin!",参见图2-2。


图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 applicationhello.jsp.title=hello - a first struts programhello.jsp.page.heading=hello world! a first struts applicationhello.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.javahello.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.javapackage 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():把属性重新设置为默认值。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表