在以前的学习中,<action>元素的配置,都是用明确的配置,其name、class等属性都是一个明确的值。其实Struts2还支持class属性和method属性使用来自name属性的通配符。

  1)接下来看看使用通配符的示例,如果我们使用以下<action>配置:

<action name="*_*" class="cn.javass.action.action.{1}Action" method="{2}">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>

在上面的配置中:

name属性的值中“*”代表长度不为0的任意字符串,因此,它可以响应的action只需要名称中间有一个下划线即可。比如页面可访问的action名称为:HelloWorld_create.action、HelloWorld _update.action等等。

在name属性定义了通配符之后,class属性使用第一个通配符(使用{1}作为占位),method属性使用第二个通配符。

如果使用HelloWorld_create.action作为访问的action名称的话,struts.xml中action名称为HelloWorld_create,第一个通配符匹配HelloWorld,第二个通配符匹配create。因此,由cn.javass.action.action.HelloWorldAction的create方法来响应。

  2)那么,对于<result>元素,能不能也使用<action>元素的name属性定义的通配符呢?答案是可以的,假如有如下的配置:

<action name="*_*_*_*" class="cn.javass.action.action.{1}Action" method="{2}">
<result name="{3}">/${folder}/{4}.jsp</result>
</action>

  3)在使用通配符的时候,也有可能不止一个使用通配符的<action>元素可能匹配这次URL的访问,看以下的配置文件:

<action name="HelloWorld_create" class="cn.javass.action.action.HelloWorldAction" method="create2">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>
<action name="*_*" class="cn.javass.action.action.{1}Action" method="{2}">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>

  这时候,如果访问“/helloworld/HelloWorld_create.action”,Struts2首先会查找是否有精确匹配的<action>元素,这时候无论以上<action>元素以什么顺序出现,Struts2肯定会先找到并使用精确匹配的<action>元素。但是,如果没有精确匹配的<action>元素,则Struts2会找到第一个匹配的使用通配符的<action>元素来使用。

  通配符对于那些简单的CRUD的工程或软件原型来说,只要Action的包名、Action的类名、对应的方法名写的有规律的应用,能大大简化配置的工作。

  参考资料:http://www.iteye.com/topic/1124526

【struts2】action中使用通配符的更多相关文章

  1. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  2. Struts2 Action中动态方法调用、通配符的使用

    一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...

  3. struts2中action中的通配符

    struts中一个正常的最普通不过的action是这样子的 <package name="default1" namespace="/gys" exten ...

  4. struts2 中属性驱动(其实就是struts2 action 中处理 request 的参数【old 方式servlet api 封装数据到javabean中(or beanutils)】),这里属性驱动是新方式

    1.属性驱动 a\  一般的set public class UserAction extends ActionSupport { private String username; private S ...

  5. struts2.Action中的method属性配置

    .Action中的method属性 在struts1.x中我们知道通过继承DispatchAction可以实现把多个Action进行统一操作,在struts2中实现action的统一操作也很简单.我们 ...

  6. Struts2 Action中的方法命名不要以get开头

    偶然发现,在调用一个action中的某个方法时,会自动调用另一个无关的方法,找了好久,最后发现是方法命名的问题,方法命名以get开头,action会自动调用!所以,以后再写action中的方法时尽量不 ...

  7. 将 jsp 页面的值 传到struts2 action中(不是表单中的值)

    JSP: 页面: <%@ page language="java"  pageEncoding="GBK"%> <%@taglib prefi ...

  8. 在Struts2 Action中快速简便的访问Request、Session等变量

    前言——正常情况下如何在Action中获取到这些变量 全部方法(共四种)可以参考:http://blog.csdn.net/itmyhome1990/article/details/7019476 这 ...

  9. struts2 action中传递两个参数到url

    <action name="outInDetail" class="formManage_outInDetailAction"> <resul ...

随机推荐

  1. 如何用Client OM获取页面上一个Content web part的内容

    [解决方法] According to Wictor Wilén, The Client Object Model is fairly limited when it comes to working ...

  2. [Algorithm] Inorder Successor in a binary search tree

    For the given tree, in order traverse is: visit left side root visit right side // 6,8,10,11,12,15,1 ...

  3. [Algorithm] Check if a binary tree is binary search tree or not

    What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...

  4. 视图控制器生命周期中各个重要的方法(Swift) (Important Methods during the Lifecycle of a View Controller)

    1. init(coder:) 它是视图控制器从故事板创建实例的默认初始化函数.(It is the initializer for UIViewController instances create ...

  5. Yahoo邮箱最后登录,成为历史!

  6. To LACP or not to LACP (on a 5.1 vDS)

    http://www.poppingclouds.com/2012/12/20/to-lacp-or-not-to-lacp-on-a-5-1-vds-2/ I have been recently ...

  7. OpenGL 核心技术之立方体贴图

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家.特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  8. 算法笔记_199:第二届蓝桥杯软件类决赛真题(C语言本科)

    前言:以下代码部分仅供参考,C语言解答部分全部来自网友,Java语言部分部分参考自网友,对于答案的正确性不能完全保证. 试题1 数论中有著名的四方定理:所有自然数至多只要用四个数的平方和就可以表示. ...

  9. 如何捕获 System.loadLibrary 产生的异常?(转)

    如何捕获 System.loadLibrary 产生的异常? 当使用以下代码时,会发现异常处理的代码根本不会被执行: try{ System.loadLibrary("SimpleAuthe ...

  10. oracle-sakila-db sakila的Oracle版本

    在Sakila——MySQL样例数据库解析 中,学习了sakila for mysql的表结构.并把它作为数据仓库.ODI ETL学习的源数据库.ODI Studio拓扑结构的创建与配置 后来发现OD ...