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. Eclipse使用代码清理功能(Clean Up)

    本文转载自http://www.ibm.com/developerworks/cn/opensource/os-eclipse-clean/ 但是为了适应自己使用,还是自己总结了一下. 一.概述 代码 ...

  2. Web Api 在线参考文档

    参考文档: https://developer.mozilla.org/zh-CN/docs/Web/API

  3. python练习程序(c100经典例10)

    题目: 打印楼梯,同时在楼梯上方打印两个笑脸. print '..' for i in range(1,9): print '**' for j in range(1,i+1): print ' ',

  4. 【自动化测试】PO思路

    http://blog.csdn.net/liubofengpython/article/details/7720078

  5. MySQL多表连接

    主要分3种:内连接,外连接,交叉连接 其        他:联合连接,自然连接 1.内联接 典型的联接运算,使用像 =  或 <> 之类的比较运算).包括相等联接和自然联接. 内联接使用比 ...

  6. AVL树的旋转实现

    AVL树:带有平衡条件的二叉查找树,即一棵AVL树是其每个节点的左子树和右子树的高度最多相差1的二叉查找树.一般通过Single Rotate和Double Rotate来保持AVL树的平衡.AVL树 ...

  7. 【转】让Souce Insight支持多种语言的语法高亮:Python,Ruby,ARM汇编,windows脚本文件(bat/batch),PPC,SQL,TCL,Delphi等

    原文网址:http://www.crifan.com/source_insight_support_highlight_for_python_ruby_arm_batch_ppc_sql_tcl_de ...

  8. MySQL与Oracle 差异比较之一数据类型

    数据类型 编号 ORACLE MYSQL 注释 1 NUMBER int / DECIMAL DECIMAL就是NUMBER(10,2)这样的结构INT就是是NUMBER(10),表示整型:MYSQL ...

  9. ubuntu切换root

    sudo passwd root这个命令是给root用户设定密码.然后su root切换到root用户.

  10. 【转】C# 委托的介绍(delegate、Action、Func、predicate)

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Delegat ...