`
tigerben
  • 浏览: 28683 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Struts Action 之 method(转)

阅读更多

1 .DispatchAction extends BaseAction
一般的Action如<action path="/createUser" type="examples.UserAction">,在这里UserAction只需要继承父类(extends Action类),然后重写父类的execute方法,在execute中实现具体的控制转向。
对于同一个formbean上进行的新增、修改、删除等,我们需要分发不同的Action,这里有两种做法。
① 一种是通过在execute方法中if判断进行不同的转向:
㈠ UserAction 类的execute方法中
String method = request.getParameter("method");
if (method.equals("create")) {
     ……
    return mapping.findForward("createUser");
}
if (method.equals("save")) {
    ……
    return mapping.findForward("saveUser");
}

㈡ struts-config.xml 中:
<action path="/createUser" type="examples.UserAction"
        name="userForm"
        scope="request">
    <forward name="createUser" path="/pages/listUser.jsp"/>
</action>
<action path="/saveUser" type="examples.UserAction"
        name="userForm"
        scope="request">
    <forward name="saveUser" path="/pages/saveUser.jsp"/>
</action>

㈢ 可以在页面中定义一个隐藏变量来指明相应的操作
// 这里最好不要使用<html:hidden property="method"/>
// 因为这种写法需要在formbean中定义相应的property,我们可以采用普通隐藏域
<input type="hidden" name="method"/>
然后定义一个javascript函数,我们可以在通过点击提交按钮的时候,在函数中修改它的值。
<script>
    function set(operation) {
        with (document.forms[0]) {
            method.value = operation;
        }
    }
</script>
点击提交按钮时,通过set方法设置提交的method属性值:
<html:submit onclick="set('create');">CREATE</html:submit>
<html:submit onclick="set('save');">SAVE</html:submit>


② 第二就是使UserAction继承DispatchAction,不需要重写execute方法:
public ActionForward create(ActionMapping mapping,
                           ActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
        throws Exception {
    // 進行一些create的逻辑
    // ……
    return mapping.findForward("createUser");
}
public ActionForward save(ActionMapping mapping,
                           ActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
        throws Exception {
    // 進行一些save的逻辑
    // ……
    return mapping.findForward("saveUser");
}

㈡ DispatchAction 在配置上和一般Action稍有不同,就是要在Action配置中多一个parametr属性,这个属性可以指定执行DispatchAction中对应的方法。
struts-config.xml 中:
<action path="/processUser" type="examples.UserAction"
        name="userForm"
        scope="request"
        parameter="method">
    <forward name="createUser" path="/pages/listUser.jsp"/>
    <forward name="saveUser" path="/pages/saveUser.jsp"/>
</action>

㈢我们在这里指定了parameter的值为method,则页面提交时我们必须指定提交时action的method参数来确定去我们想要调用哪个Action方法。
<script>
    function submitForm(operation) {
        with (document.forms[0]) {
            action = action + '?method = '+ operation;
            submit();
        }
    }
</script>
点击提交按钮时,通过submitForm方法设置提交时action的method参数:
<html:form action="/processUser" method="get">
<html:button onclick="submitForm('create');">CREATE</html:button>
<html:button onclick="submitForm('save');">SAVE</html:button>
</html:form>

2 . LookupDispatchAction extends DispatchAction
LookupDispatchAction 继承DispatchAction, 在上面的 ② ㈢中对于同一个页面上的多个submit按钮,不需要那么多复杂的js函数来指定提交时action的method参数,即上面的submitForm(operation)方法可以省去,LookupDispatchAction其用法为:
① 用MessageResource将按钮的文本和ResKey相关联,例如button.save=保存;中用LookupDispatchAction代替就是: ② ㈢
<html:form action="/processUser" method="get">
<html:submit property=" method ">
    <bean:message key=" button.create "/>
</html:submit>
<html:submit property=" method ">
    <bean:message key=" button.save "/>
</html:submit>
</html:form>

② 在Action配置中多一个parametr属性,属性值与submit按钮的property属性值相同,这个属性可以指定执行LookupDispatchAction中对应的方法。
struts-config.xml 中:
<action path=" /processUser " type="examples.UserAction"
        name="userForm"
        scope="request"
        parameter=" method ">
    <forward name="createUser" path="/pages/listUser.jsp"/>
    <forward name="saveUser" path="/pages/saveUser.jsp"/>
</action>


使UserAction继承LookupDispatchAction,重写getKeyMethodMap()方法, 将ResKey和MethodName对应起来, 如下:
protected Map getKeyMethodMap() {
    Map map = new HashMap();
    map.put("button.create", "create");
    map.put("button.save", "save");
    return map;
}

注: DispatchAction 类使用请求参数的值确定调用哪种方法,而LookupDispatchAction类利用请求参数值,反向查询资源绑定,并将它与类中的一种方法匹配,实际上这两种方法有异曲同工之处。

3 . MappingDispatchAction extends DispatchAction
DispatchAction 指定了parameter的值为method,则页面提交时我们必须指定提交时action的method参数来确定去我们想要调用哪个Action方法,而MappingDispatchAction直接通过struts-config.xml将多个action-mapping映射到同一个Action类的不同方法:
<action path="/createUser" type="examples.UserAction"
        name="userForm"
        scope="request"
        parameter="create">
    <forward name="createUser" path="/pages/listUser.jsp"/>
</action>
<action path="/saveUser" type="examples.UserAction"
        name="userForm"
        scope="request"
        parameter="save">
    <forward name="saveUser" path="/pages/saveUser.jsp"/>
</action>

然后UserAction继承MappingDispatchAction即可:
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

注: 查看MappingDispatchAction的源码:
protected String getMethodName(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response,
    String parameter) throws Exception {
    // Return the unresolved mapping parameter.
    return parameter;
}
可以看到它调用的方法是直接返回struts-config.xml中parameter的值。

4 . ForwardAction extends BaseAction
相当于<jsp:forward>功能,不需要配置formbean和action,可以直接进行跳转,只需要在struts-config.xml中配置:
<action path="/listUser"
        type="org.apache.struts.actions.ForwardAction"
        scope="request"
        parameter="/pages/listUser.jsp">
</action>
parameter 属性用于指定forward到哪个页面,path、type、parameter三个属性为必须,其他可省略。

5 . IncludeAction extends BaseAction
相当于<jsp:include>功能,需要在struts-config.xml中配置:
<action path="/listUser" type="org.apache.struts.actions.IncludeAction"
        name="userForm"
        scope="request"
        parameter="/pages/includepage.jsp">
</action>
分享到:
评论

相关推荐

    struts2 result转向到action

    &lt;form id="form1" name="form1" method="post" action="/login.action"&gt; 用户: 密码: ``` 这段代码定义了一个 HTML 表单,其中包含两个文本框(用户名和密码)以及一个提交按钮。当用户填写完...

    struts2.0之action

    Struts2.0是Java Web开发中的一个框架,它的核心组件是Action,它负责处理用户的请求并进行业务逻辑处理。相较于Struts 1.x,Struts 2.0的Action模型有着显著的区别。 在Struts 1.x中,Action类需要继承`org.apache...

    Struts2 Action参数详细说明

    Struts2框架中的Action是核心组件之一,它负责接收HTTP请求并进行业务逻辑处理,然后将控制转向相应的视图进行展示。在Struts2中,Action的配置和使用方式有多种,下面将详细介绍Struts2 Action参数的详细说明。 ...

    struts2动态访问调用-method方法

    Struts2作为一款流行的Java Web框架,其动态访问调用机制是其强大特性的体现之一。在实际开发中,我们经常需要根据用户的不同操作来执行不同的业务逻辑,这时`struts2`的`method`属性就显得尤为重要。本文将深入探讨...

    struts2 action跳转action传参数

    ### Struts2中Action间的参数传递方法 在Struts2框架中,经常需要实现Action之间的跳转,并在跳转过程中传递必要的参数。这种需求在实际开发中非常常见,尤其是在需要根据用户的不同操作来调用不同的业务逻辑时。...

    struts2一个action处理多个请求 struts2实例

    在Struts2框架中,Action类是业务逻辑处理的核心组件,它负责接收并处理来自用户的请求。本实例探讨了如何让一个Action类处理多个请求,这在开发中常见于需要集中处理相似请求的情况,可以提高代码复用性和结构的...

    Struts2_0500_ActionMethod

    2. 请求映射:Struts2的DispatcherServlet接收到请求后,通过Struts2的配置文件(struts.xml或struts.properties)找到对应的Action配置,确定要执行的Action类及其Action方法。 3. Action实例化:Struts2使用IoC...

    Struts2中struts_xml的Action配置详解

    在`struts.xml`中,一个Action配置通常由`&lt;action&gt;`元素定义,包含了多个属性,如`name`、`class`、`method`等。 `name`属性用于定义Action的唯一标识,它在请求URL中出现,用于区分不同的Action。例如: ```xml ...

    struts 2 action 动态调用

    如上所示,我们添加了`method="doAdd"`属性,这意味着当访问`test.action`时,Struts 2将调用`doAdd()`方法。 **5. JSP 页面显示** 在JSP页面中,可以使用EL表达式来获取Action中设置的值。 ```jsp ; charset=...

    struts2 使用action属性接收中文参数(post提交)

    在处理用户请求时,Struts2允许开发者通过Action类来接收和处理参数,包括中文参数。当我们需要通过POST方法提交包含中文字符的数据时,可能会遇到编码问题,因为HTTP请求默认使用的是ASCII编码,而中文字符需要UTF-...

    struts2 action的三种访问方式

    在Struts2中,Action是处理用户请求的核心组件。它负责业务逻辑的执行,并将结果返回给视图进行展示。本篇文章将详细介绍Struts2 Action的三种访问方式:传统方式、通配符方式和动态方式。 1. **传统方式(Static ...

    Struts2_Action学习笔记、通配符{1},{2}

    通过上述分析可以看出,Struts2提供了丰富的特性来支持Web应用程序的开发,尤其是通过Namespace和ActionMethod等机制来组织和管理Action,以及利用通配符配置来简化配置文件,大大提高了开发效率和维护性。

    struts2笔记之动态调用Action指定方法及默认Action

    其次,我们可以在 struts.xml 文件中配置一个 Action,并指定 method 属性,例如:&lt;action name="register" class="com.LoginAction" method="register"&gt;&lt;/action&gt;这时,我们可以在 JSP 文件中使用 `&lt;s:form action=...

    Struts2上传和下载Action配置

    在Struts2中,文件上传和下载是通过Action类进行配置和处理的,让我们一起深入了解一下这个过程。 首先,我们要理解上传的流程。在用户端,通常通过HTML表单来选择要上传的文件,表单的`enctype`属性必须设置为`...

    struts中dispatchAction用法

    Struts是Java Web开发中的一款经典MVC框架,它的核心组件之一是DispatchAction,这个组件在处理用户请求时起到了至关重要的作用。DispatchAction是Struts提供的一个可扩展的Action类,它允许开发者将一个单一的...

    Struts2的Action讲解

    ### Struts2的Action详解 #### 一、引言 ...&lt;action name="AliasHelloWorld" class="tutorial.HelloWorld" method="aliasAction"&gt; &lt;result&gt;/HelloWorld.jsp &lt;/action&gt; ``` 或者在请求URL中指定方法名: ``` ...

    Struts2_Action

    通过在action标签中指定method属性可以实现动态方法调用。 以上内容介绍了Struts2 Action的核心知识点,包括其概念、配置方法、Action类的编写以及与Struts2框架的交互方式。在实际开发中,开发者可以根据具体需求...

    Struts2的Action多个方法配置和验证

    这篇博客主要讨论了如何配置Struts2 Action中的多个方法以及如何进行数据验证。 首先,一个Action类可以包含多个方法,每个方法对应一个特定的HTTP请求。这种多方法配置允许我们根据不同的请求路径来调用不同的业务...

    struts2利用通配符调用同一个Action里面不同的方法

    在Struts2中,Action是业务逻辑处理的核心,而通配符的使用则是Struts2框架中一种灵活的配置方式,允许我们以更简洁的方式调用同一个Action中的不同方法。下面我们将深入探讨如何利用Struts2的通配符来实现这一功能...

    struts2的类型转换详解

    &lt;form action="User.action" method="post"&gt; 用户名: 密码: 年龄: 生日: 爱好: 篮球 排球 ``` 在这个例子中,Struts2会自动尝试将`uname`、`upass`、`age`和`birthday`字段的字符串值转换为对应的用户...

Global site tag (gtag.js) - Google Analytics