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. ionic2rc版常见的一些坑

    1.config.xml里的包名不能有横杠,否则在build android的时候会报错 <widget id="com.ionicframework.name-abc" v ...

  2. 浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  3. IntelliJ IDEA使用总结篇

    解决控制台中文乱码的问题: 1.windows下改intellij安装目录下bin\idea.exe.vmoptions文件 加上 -Dfile.encoding=UT 2.设置IDEA server ...

  4. erl0007 - erlang 远程节点连接的两种方式

    启动连接:erl -setcookie abc -name xxx@192.168.x.x -remsh xxx@192.168.x.y 退出:ctrl + g,q 参考:http://www.cnb ...

  5. Android使用绘图Path总结

    Path作为Android中一种相对复杂的绘图方式,官方文档中的有些解释并不是很好理解,这里作一个相对全面一些的总结,供日后查看,也分享给大家,共同进步. 1.基本绘图方法 addArc(RectF ...

  6. Android中的事件分发和处理

    上次跟大家分享了一下自定义View的一下要点,这次跟大家聊一下View的事件分发及处理,为什么主题都是View,因为作为一名初级应用层Android工程师,跟我打交道最多的莫过于各种各样的View,只 ...

  7. MySQL与Oracle 差异比较之四条件循环语句

    循环语句 编号 类别 ORACLE MYSQL 注释 1 IF语句使用不同 IF iv_weekly_day = 'MON' THEN       ii_weekly_day := 'MON';ELS ...

  8. 嵌入式 hi3518平台指定网卡测试是否通外网

    版权声明:本文为博主原创文章,未经博主允许不得转载. /********************************** (C) COPYRIGHT *********************** ...

  9. WebAPI初探

    由于即将要接手的新项目计划用ASP.NET MVC3来开发,所以最近一段时间一直在看相关的书或文章.因为之前在大学里也曾学习过MVC2开发,也做过几个简单的MVC2的小型测试项目,不过在后来工作以后主 ...

  10. 自定义View绘制字符串

    import android.app.Activity; import android.os.Bundle; import android.view.Display; import android.v ...