Struts wildcards can helps to reduce the repetition in your struts-config.xml file, as long as your Struts project is following some regular file structure. For example, in User module, to implement the CRUD function, your struts-config.xml may look like following

1. No Wildcards

You need to create four action mappings for each list, add, delete and update function, and a lot of repetition.

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action
path="/ListUserAction"
type="com.mkyong.common.action.UserAction"
parameter="ListUser"
> <forward name="success" path="/pages/ListUser.jsp"/> </action> <action
path="/AddUserAction"
type="com.mkyong.common.action.UserAction"
parameter="AddUser"
> <forward name="success" path="/pages/AddUser.jsp"/> </action> <action
path="/EditUserAction"
type="com.mkyong.common.action.UserAction"
parameter="EditUser"
> <forward name="success" path="/pages/EditUser.jsp"/> </action> <action
path="/DeleteUserAction"
type="com.mkyong.common.action.UserAction"
parameter="DeleteUser"
> <forward name="success" path="/pages/DeleteUser.jsp"/> </action> </action-mappings> </struts-config>

2. With Wildcards

With Struts wildcards feature, your struts-config.xml can cut down into one action mapping.

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action
path="/*UserAction"
type="com.mkyong.common.action.UserAction"
parameter="{1}User"
> <forward name="success" path="/pages/{1}User.jsp"/> </action> </action-mappings> </struts-config>

Let’s see an use case, try access via http://localhost:8080/StrutsExample/EditUserAction.do. The “EditUserAction.do” will match the “/*UserAction” pattern, and the * matched string “Edit” is represent by {1} for later use.

In above case, the wildcards action mapping will change from

       <action
path="/*UserAction"
type="com.mkyong.common.action.UserAction"
parameter="{1}User"
> <forward name="success" path="/pages/{1}User.jsp"/> </action>

to

       <action
path="/EditUserAction"
type="com.mkyong.common.action.UserAction"
parameter="EditUser"
> <forward name="success" path="/pages/EditUser.jsp"/> </action>

Conclusion

Both struts-config.xml samples have the same functionality, but with less repetition in wildcards support. However, DO NOT overuse this wildcards feature in your project, it’s less manageable than the normal declaration.

Struts – Wildcards example的更多相关文章

  1. struts.enable.DynamicMethodInvocation = true 动态方法调用

    default.properties 在Struts 2的核心jar包-struts2-core中,有一个default.properties的默认配置文件.里面配置了一些全局的信息,比如: stru ...

  2. struts.enable.DynamicMethodInvocation = true 动态方法调用(转)

    原文地址:http://blog.csdn.net/wfcaven/article/details/5937557 default.properties 在Struts 2的核心jar包-struts ...

  3. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  4. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  5. Struts框架的核心业务

    Struts的核心业务 Struts核心业务有很多,这里主要介绍了比较简单一些的: 请求数据的处理,和数据自动封装,类型自动转换 1.Struts中数据处理 1.1.方式1:直接过去servletap ...

  6. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  7. 配置hibernate,Struts。文件

    hibernate文件配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernat ...

  8. hibernate与Struts框架结合编写简单针对修改练习

    失败页面fail.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  9. 3. 解析 struts.xml 文件

    1. struts.xml 文件基本配置: 主要放在资源路径下,配置 sturts2相关的 Action , 拦截器等配置 <struts> <!-- 设置常量 --> < ...

随机推荐

  1. typedef struct trx_struct trx_t;

    /* The transaction handle; every session has a trx object which is freed only when the session is fr ...

  2. jQuery_效果(隐藏和显示)

    一.jQuery hide() 显示和 show()隐藏 通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素: <script type=" ...

  3. Qt之等待提示框(QTimer)

    简述 上节讲述了关于QPropertyAnimation实现等待提示框的显示,本节我们使用另外一种方案来实现-使用定时器QTimer,通过设置超时时间定时更新图标达到旋转效果. 简述 效果 资源 源码 ...

  4. 51nod1346 递归

    我终于知道我有多么蠢了...推规律根本不带我这么推的...跟51nod那场比赛的傻逼B题一样,想都不想想就打表找规律...智障啊找规律也要按照基本法! //f[1][2]=a[1][2] f[2][1 ...

  5. IE6,7下li标签的间隙

    1.在IE6,7下li本身没浮动,但是li内容有浮动的时候,li下边就会产生3px的间隙. 解决办法: 1.给li加浮动 2.给li加vertical-align:top; eg: <!DOCT ...

  6. C#中的lock关键字;就是lock住一个大家都共同访问的(静态static的)东东就行了

    public class ChatService : IChat //继承IChat接口或者说IChat的实现类 { //定义一个静态对象用于线程部份代码块的锁定,用于lock操作 private s ...

  7. MYSQL复制的几种模式

    MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制. MYSQL复制的几种模式 MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制.简言之,这种新技 ...

  8. Android 多进程编程 15问15答!

    ps:阅读本文 需要对android 多进程编程有一定了解. 1.Android中总共有几种方式进行IPC? 答:一共有两种,一种是binder 还有一种是socket.Binder 大家用的比较多. ...

  9. Jquery Mobile设计Android通讯录第二章

    本文是jQuery Mobile设计Android通讯录系统教程的第二篇,在上一篇教程中(http://publish.itpub.net/a2011/0517/1191/000001191561.s ...

  10. JAVA 锁

    JAVA 锁 锁的概念 Java中的锁是控制资源访问的一种方式.它弥补了synchronized的可操作性不强的不足. Java的锁都实现了Lock接口.Lock结构定义了锁的基本操作. 函数 解释 ...