struts中的logic:present标签 用于实现分页功能(转)

来源:互联网 发布:淘宝猜你喜欢在哪里 编辑:程序博客网 时间:2024/05/20 14:15

在action中:

request.getSession().setAttribute("pageBean", pageBean);

request.setAttribute("result", pageBean.getObjList());

在jsp中:
<logic:present >
       <logic:iterate >
            <logic:present >
                <tr>
                    <td><bean:write property="name" /></td>
                    <td><bean:write property="author" /></td>
                    <td><bean:write property="price" /></td>
                 </tr>
             </logic:present>
        </logic:iterate>
</logic:present>

<logic:present scope="session">这里有个scope属性可能是request,session等,name指的是一个对象,可能是 javaBean,也可能是集合的对象,整个意思就是就是在scope(session,request)范围里面查找name(一个对象)是否存在,相当于一个if()语句.

本章讲解了一个简单的Struts应用例子helloapp应用,这个例子可以帮助读者迅速入门,获得开发Struts应用的基本经验。该应用的功能非常简单,接受用户输入的姓名<name>,然后输出“Hello <name>”。开发helloapp应用涉及以下内容:

l         分析应用需求

l         把基于MVC设计模式的Struts框架运用到应用中

l         创建视图组件,包括HTML表单(hello.jsp)和ActionForm Bean(HelloForm.java)

l         创建application.properties资源文件

l         数据验证,包括表单验证和业务逻辑验证

l         创建控制器组件: HelloAction.java

l         创建模型组件: PersonBean.java

l         创建包含被各个模块共享的常量数据的Java文件: Constants.java

l         创建配置文件:web.xml和struts-config.xml

l         编译、发布和运行helloapp应用

2.1 分析helloapp应用的需求
在开发应用时,首先从分析需求入手,列举该应用的各种功能,以及限制条件。helloapp应用的需求非常简单,包括如下需求:

l         接受用户输入的姓名<name>,然后返回字符串“Hello <name> !”

l         如果用户没有输入姓名就提交表单,将返回出错信息,提示用户首先输入姓名。

l         如果用户输入姓名为“Monster”,将返回出错信息,拒绝向“Monster”打招呼。

l         为了演示模型组件的功能,本应用使用模型组件来保存用户输入的姓名。

2.2 运用Struts框架
下面把Struts框架运用到helloapp应用中。Struts框架可以方便迅速的把一个复杂的应用划分成模型、视图和控制器组件,而Struts的配置文件struts-config.xml则可以灵活的组装这些组件,简化开发过程。

以下是helloapp应用的各个模块的构成:

l         模型包括一个JavaBean组件PersonBean,它有一个userName属性,代表用户输入的名字。它提供了get/set方法,分别用于读取和设置userName属性,它还提供一个save()方法,负责把userName属性保存到持久化存储系统中,如数据库或文件系统。对于更为复杂的 Web应用,JavaBean组件可以作为EJB或Web服务的前端组件。

l         视图包括一个JSP文件hello.jsp,它提供用户界面,接受用户输入的姓名。视图还包括一个ActionForm Bean,它用来存放表单数据,并进行表单验证,如果用户没有输入姓名就提交表单,将返回出错信息。

l         控制器包括一个Action类HelloAction,它完成三项任务:1.进行业务逻辑验证,如果用户输入的姓名为“Monster”, 将返回错误消息;2.调用模型组件PersonBean的save()方法,保存用户输入的名字;3.决定将合适的视图组件返回给用户。

除了创建模型、视图和控制器组件,还需要创建Struts的配置文件struts-config.xml,它可以把这些组件组装起来,使它们协调工作。此外,还需要创建整个Web应用的配置文件web.xml。

2.3 创建视图组件
本例中,视图包括两个组件:

l         一个JSP文件:hello.jsp

l         一个ActionForm Bean: HelloForm Bean

下面分别讲述如何创建这两个组件。

2.3.1 创建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文件有以下特点:

l         没有任何Java程序代码

l         使用了许多Struts的客户化标签,例如<html:form>和<logic:present>标签

l         没有直接提供文本内容,取而代之的是<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>:

l         <html:errors>:用于显示Struts框架中其他组件产生的错误消息。

l         <html:form>:用于创建HTML表单,它能够把HTML表单的字段和ActionForm Bean的属性关联起来。

l         <html:text>:该标签是<html:form>的子标签,用于创建HTML表单的文本框。它和ActionForm Bean的属性相关联。

hello.jsp中使用了来自Struts Bean标签库的两个标签<bean:message>和<bean:write> :

l         <bean:message>:用于输出本地化的文本内容,它的key属性指定消息key,和消息key匹配的文本内容来自于专门的 Resource Bundle,关于Resource Bundle的概念参见本书第9章(Struts应用的国际化)。

l         <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在特定的范围内不存在,才会执行标签主体中的内容。

2.3.2 创建消息资源文件
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”输出到网页上。

2.3.3 创建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的常规方法,还有两个特殊方法:

l         validate():用于表单验证。

l         reset():把属性重新设置为默认值。

2.3.4 数据验证
几乎所有和用户交互的应用都需要数据验证,而从头设计并开发完善的数据验证机制往往很费时。幸运的是,Struts框架提供了现成的、易于使用的数据验证功能。Struts框架的数据验证可分为两种类型:表单验证和业务逻辑验证,在本例中,它们分别运用于以下场合:

l         表单验证:如果用户没有在表单中输入姓名,就提交表单,将生成表单验证错误

l         业务逻辑验证:如果用户在表单中输入的姓名为“Monster”,按照本应用的业务规则,不允许向“Monster”打招呼,因此将生成业务逻辑错误。

第一种类型的验证,即表单验证由ActionForm Bean来负责处理。在本例中,HelloForm.java的validate()方法负责完成这一任务:

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;

    }

}

当用户提交了HTML表单,Struts框架自动把表单数据组装到ActionForm Bean中。接下来Struts框架会自动调用ActionForm Bean的validate()方法进行表单验证。如果validate()方法返回的ActionErrors 对象为null,或者不包含任何ActionMessage对象,就表示没有错误,数据验证通过。如果ActionErrors中包含 ActionMessage对象,就表示发生了验证错误,Struts框架会把ActionErrors对象保存到request范围内,然后把请求转发到恰当的视图组件,视图组件通过<html:errors>标签把request范围内的ActionErrors对象中包含的错误消息显示出来,提示用户修改错误。


在Struts早期的版本中,使用ActionError类来表示错误消息,ActionError类是ActionMessage的子类。Struts1.2将废弃ActionError,统一采用ActionMessage类来表示正常或错误消息。


第二种类型的验证,即业务逻辑验证,由Action来负责处理,参见本章2.4.3节。

2.4 创建控制器组件
控制器组件包括ActionServlet类和Action类。ActionServlet类是Struts框架自带的,它是整个Struts框架的控制枢纽,通常不需要扩展。Struts框架提供了可供扩展的Action类,它用来处理特定的HTTP请求,例程2-4为HelloAction类的源程序。

例程2-4 HelloAction.java

package hello;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.util.MessageResources;

public final class HelloAction extends Action {

    /**

     * Process the specified HTTP request, and create the corresponding HTTP

     * response (or forward to another web component that will create it).

     * Return an <code>ActionForward</code> instance describing where and how

     * control should be forwarded, or <code>null</code> if the response has

     * already been completed.

     */

    public ActionForward execute(ActionMapping mapping,

                                 ActionForm form,

                                 HttpServletRequest request,

                                 HttpServletResponse response)

    throws Exception {

        // These "messages" come from the ApplicationResources.properties file

        MessageResources messages = getResources(request);

        /*

         * Validate the request parameters specified by the user

         * Note: Basic field validation done in HelloForm.java

         *       Business logic validation done in HelloAction.java

         */

        ActionMessages errors = new ActionMessages();

        String userName = (String)((HelloForm) form).getUserName();

        String badUserName = "Monster";

        if (userName.equalsIgnoreCase(badUserName)) {

           errors.add("username", new ActionMessage("hello.dont.talk.to.monster",

badUserName ));

           saveErrors(request, errors);

           return (new ActionForward(mapping.getInput()));

        }

        /*

         * Having received and validated the data submitted

         * from the View, we now update the model

         */

        PersonBean pb = new PersonBean();

        pb.setUserName(userName);

        pb.saveToPersistentStore();

        /*

         * If there was a choice of View components that depended on the model

         * (or some other) status, we'd make the decision here as to which

         * to display. In this case, there is only one View component.

         *

         * We pass data to the View components by setting them as attributes

         * in the page, request, session or servlet context. In this case, the

         * most appropriate scoping is the "request" context since the data

         * will not be neaded after the View is generated.

         *

         * Constants.PERSON_KEY provides a key accessible by both the

         * Controller component (i.e. this class) and the View component

         * (i.e. the jsp file we forward to).

         */

        request.setAttribute( Constants.PERSON_KEY, pb);

        // Remove the Form Bean - don't need to carry values forward

        request.removeAttribute(mapping.getAttribute());

        // Forward control to the specified success URI

        return (mapping.findForward("SayHello"));

    }

}

HelloAction.java是本应用中最复杂的程序,下面分步讲解它的工作机制和流程。

2.4.1 Action类的工作机制
所有的Action类都是org.apache.struts.action.Action的子类。Action子类应该覆盖父类的execute() 方法。当ActionForm Bean被创建,并且表单验证顺利通过后, Struts框架就会调用Action类的execute()方法。execute()方法的定义如下:

public ActionForward execute(ActionMapping mapping,

                  ActionForm form,

                  HttpServletRequest request,

                  HttpServletResponse response) throws IOException, ServletException ;

execute()方法包含以下参数:

l         ActionMapping:包含了这个Action的配置信息,和struts-config.xml文件中的<action>元素对应。

l         ActionForm:包含了用户的表单数据,当Struts框架调用execute()方法时,ActionForm中的数据已经通过了表单验证。

l         HttpServletRequest:当前的HTTP请求对象

l         HttpServletResponse:当前的HTTP响应对象

Action类的execute()方法返回ActionForward对象,它包含了请求转发路径信息。

2.4.2 访问封装在MessageResources中的本地化文本
在本例中,Action类的execute()方法首先获得MessageResources对象:

MessageResources messages = getResources(request);

在Action类中定义了getResources(HttpServletRequest request)方法,该方法返回当前默认的MessageResources对象,它封装了Resource Bundle中的文本内容。接下来Action类就可以通过MessageResources对象来访问文本内容。例如,如果要读取消息key为 “hello.jsp.title”对应的文本内容,可以调用MessageResources类的getMessage(String key)方法:

String title=messages.getMessage("hello.jsp.title");

2.4.3 业务逻辑验证
接下来,Action类的execute()方法执行业务逻辑验证:

ActionMessages errors = new ActionMessages();

String userName = (String)((HelloForm) form).getUserName();

String badUserName = "Monster";

if (userName.equalsIgnoreCase(badUserName)) {

   errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName ));

   saveErrors(request, errors);

   return (new ActionForward(mapping.getInput()));

}

如果用户输入的姓名为“Monster”,将创建包含错误信息的ActionMessage对象,ActionMessage对象被保存到 ActionMessages对象中。接下来调用在Action基类中定义的saveErrors()方法,它负责把ActionMessages对象保存到request范围内。最后返回ActionForward对象,Struts框架会根据ActionForward对象包含的转发信息把请求转发到恰当的视图组件,视图组件通过<html:errors>标签把request范围内的ActionMessages对象中包含的错误消息显示出来,提示用户修改错误。

在2.3.4节还提到了ActionErrors对象,图2-3显示了ActionMessages、ActionErrors、 ActionMessage和ActionError类的类框图。ActionErrors继承ActionMessages,ActionError继承ActionMessage,ActionMessages和ActionMessage之间为聚集关系,即一个ActionMessages对象中可以包含多个ActionMessage对象。


图2-3 ActionMessages、ActionErrors、ActionMessage和ActionError类的类框图

表单验证通常只对用户输入的数据进行简单的语法和格式检查,而业务逻辑验证会对数据进行更为复杂的验证,很多情况下,需要模型组件的介入,才能完成业务逻辑验证。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kele1121/archive/2009/10/29/4741900.aspx

原创粉丝点击