源自:Struts2的动态Action实现

在Struts2中动态方法调用有三种方式。

一、指定method属性
在struts.xml中指定action的method属性。

<package name=”demo1” extends=”struts-default”>
<action name=”Login” class=”com.demo.LoginAction” />
<result name=”input”>/input.jsp</result>
<result name=”error”>/error.jsp</result>
<result name=”success”>/success.jsp</result>
</action>
<action name=”Registry” class=”com.demo.LoginAction” method=”registry” />
<result name=”input”>/input.jsp</result>
<result name=”error”>/error.jsp</result>
<result name=”success”>/success.jsp</result>
</action>
</package>

处理逻辑通过method方法指定,其中名为Login的Action对应的处理逻辑为默认的execute方法,而名为Registry的Action对应的处理逻辑则为method指定的registry方法。

二、感叹号方式(需要开启),官网不推荐使用这种方式,建议大家不要使用。

三、通配符方式(官网推荐使用)

在配置<action>元素时,需要指定name、class、method等属性,这3个属性都支持通配符,在使用通配符定义Action的name属性时,相当于一个元素action定义多个逻辑Action。

第一种方法中的action配置可以更改为:

<package name=”demo” extends=”struts-default”>
<action name=”*Action” class=”com.demo.LoginAction” method=”{1}”>
<result name=”input”>/input.jsp</result>
<result name=”error”>/error.jsp</result>
<result name=”success”>/success.jsp</result>
</action>
</package>

上述定义不是定义了一个普通的action,而是定义一系列的action,只要用户请求的URL满足*Action的模式,都可通过该Action进行处理,而method属性使用了一个表达式{1},该表达式的值就是name属性中第一个*的所代表的值,例如,用户请求的URL为LoginAction,则*所代表的是Login字符串,故调用com.demo.LoginAction类的Login方法来处理。如果请示的URL为RegistryAction的话,则调用com.demo.LoginAction的Registry方法进行处理。

以下配置在class属性中使用通配符:

<package name=”demo” extends=”struts-default”>
<action name=”*Action” class=”com.demo.{1}Action”>
<result name=”input”>/input.jsp</result>
<result name=”error”>/error.jsp</result>
<result name=”success”>/success.jsp</result>
</action>
</package>

此配置中没有指定method属性,所以请示由默认的execute方式来执行,但class中使用了通配符,它的含义与上面一样,例如,当用户请求的为LoginAction时,其中*的值为Login,该值传入class属性,即该Action的处理类为com.demo.LoginAction;而如果请求为RegistryAction时,则该Action的处理类将变为com.demo.RegistryAction。

Struts2允许在class属性和method属性中同时使用表达式,示例如下:

<action name=”*_*” class=”com.demo.{1} method=”{2}” />

只要满足*_*模式的Action都会被其处理,例如有Order_Booking请求到来,由于第一个*的值为Order,第二个*的值为Booking,那么意味着将会调用com.demo.Order处理类中的Booking方法来处理用户请求。

Struts2在<result>元素中也可以使用表达式,如下:

<action name=”*Action” class=”com.demo.{1}Action method=”{1}” >
<result name=”success”>/{1}.jsp</result>
</action>

当请求为LoginAction时,将调用com.demo.LoginAction处理类中的Login方法处理用户的请求,当返回为success时,显示/Login.jsp页面。

注意:在使用通配符后,除非请求的URL与Action的name属性绝对相同,否则将按Action在struts.xml中定义的先后顺序来决定由哪个Action来处理用户请求。

Struts2的动态Action实现的更多相关文章

  1. Struts2的动态Action和全局跳转视图以及配置各项默认值

    1:Struts2的默认访问后缀是.action(特别需要注意的是改了配置文件web.xml或者struts.xml需要重启服务器) 2:Struts2中常用的常量介绍:<!-- 一:全局配置 ...

  2. struts2 动态Action

    1.java 2.struts.xml struts2.5,默认关闭动态Action,着色的是开启和使用动态action 3.JSP 小结:访问时,用!后跟方法名的方法,方法返回值----->r ...

  3. Struts2的动态方法,及result跳转方式,全局结果以及默认的action的配置

    Action动态方法的调用 首先我们需要在struts.xml中去配置一个常量值如下 那么去哪找呢?找到Struts-core.jar并打开 method属性 <action name=&quo ...

  4. Struts2.3动态调用报 No result defined for action 错误

    struts 2.3.16  採用动态调用发现不工作报404 not found,网上查找原因: 1.由于:struts2中默认不同意使用DMI 所以:须要在配置文件里打开: <constant ...

  5. 【学习笔记】Struts2之一个Action包含多个控制处理逻辑

    一.使用Action的动态方法调用     如果一个页面包含多个按钮,系统分别提交给Action的不同方法处理.此时可以采用DMI(Dynamic Method Invocation,动态方法调用)来 ...

  6. Struts2中动态方法的调用

    Struts2中动态方法调用就是为了解决一个action对应多个请求的处理,以免action太多. 主要有一下三种方法:指定method属性.感叹号方式和通配符方式.推荐使用第三种方式. 1.指定me ...

  7. 用js模拟struts2的多action调用

    近期修了几个struts2.1升级到2.3后动态方法调用失效的bug,深有感悟, 原始方法能够參考我之前的博文:struts2.1升级到2.3后动态调用方法问题 可是我那种原始方法有一个局限,就是在s ...

  8. web10 动态action的应用

    电影网站:www.aikan66.com 项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.co ...

  9. struts2的动态方法调用(DMI)和通配符映射

    动态方法调用   1.Struts2默认关闭DMI功能,需要使用需要手动打开,配置常量 struts.enable.DynamicMethodInvocation = true 2.使用“!”方法,即 ...

随机推荐

  1. AC日记——The Shortest Path in Nya Graph hdu 4725

    4725 思路: 拆点建图跑最短路: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  2. 这种文件别打开, 大小不足1KB, 却可以让你电脑瘫痪

    今年6月份,抖音表白代码火了,不足1kb的txt文件,玩出了新花样.可是你知道吗,这种非常“浪漫”的表白方式,其实存在着很大的风险,甚至会让你的电脑直接瘫痪. 首先,先说一下所谓的表白代码是怎么回事. ...

  3. mp4文件DASH切片程序

    mp4文件DASH切片程序 一.简介 按照DASH标准文档要求与现有的DASH切片(生成DASH切片参见mb4box命令简介)来生成Initialization Segment与Media Segme ...

  4. Codeforces 1099 A. Snowball-暴力(Codeforces Round #530 (Div. 2))

    A. Snowball time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. ubuntu14.04下安装爬虫工具scrapy

    scrapy是目前准备要学习的爬虫框架,其在ubuntu14.04下的安装过程如下: ubuntu14.04下默认安装了2.7的python以及setuptools,若未安装,可通过下面指令安装: s ...

  6. python基础之包与logging模块

    包 1.什么是包? 包是模块的一种形式,包的本质就是一个含有__init__.py文件的文件夹 2.为什么要有包? 提高程序的结构性和可维护性 3.如何使用包? 导入包就是在导包下的__init__. ...

  7. HDU 5289 Assignment rmq

    Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...

  8. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  9. ORACLE启动 切换实例命令

    启动服务器的其他实例 export ORACLE_SID=数据库实例名 sqlplus /nolog conn /as sysdba select name from v$database; !lsn ...

  10. SNAT的作用是什么

    SNAT,可能有人觉得奇怪,好好的为什么要进行ip地址转换啊,为了弄懂这个问题,我们要看一下局域网用户上公网的原理,假设内网主机A(192.168.2.8)要和外网主机B(61.132.62.131) ...