Struts – Wildcards example
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的更多相关文章
- struts.enable.DynamicMethodInvocation = true 动态方法调用
default.properties 在Struts 2的核心jar包-struts2-core中,有一个default.properties的默认配置文件.里面配置了一些全局的信息,比如: stru ...
- struts.enable.DynamicMethodInvocation = true 动态方法调用(转)
原文地址:http://blog.csdn.net/wfcaven/article/details/5937557 default.properties 在Struts 2的核心jar包-struts ...
- 菜鸟学Struts2——Struts工作原理
在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...
- Struts的拦截器
Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...
- Struts框架的核心业务
Struts的核心业务 Struts核心业务有很多,这里主要介绍了比较简单一些的: 请求数据的处理,和数据自动封装,类型自动转换 1.Struts中数据处理 1.1.方式1:直接过去servletap ...
- Struts的文件上传下载
Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...
- 配置hibernate,Struts。文件
hibernate文件配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernat ...
- hibernate与Struts框架结合编写简单针对修改练习
失败页面fail.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- 3. 解析 struts.xml 文件
1. struts.xml 文件基本配置: 主要放在资源路径下,配置 sturts2相关的 Action , 拦截器等配置 <struts> <!-- 设置常量 --> < ...
随机推荐
- duilib库分析: 消息流程分析
转 看下CWindowWnd类与CPaintManagerUI类是咋进行消息分发的吧. 1. 先看下CPaintManagerUI类的MessageLoop函数: void CPaintManag ...
- swift2.0 Cannot assign a value of type '[CFString]' to a value of type '[String]'
Cannot assign a value of type '[CFString]' to a value of type '[String]' 代码示例如下: picker.mediaTypes = ...
- HDU 3342 Legal or Not (图是否有环)
题意: 给出n个人的师徒关系,如有 a是b的师傅,b是c的师傅,c是a的师傅,这样则不合法,输出NO,否则输出YES. 思路: 每段关系可以看成一条有向边,从师傅指向徒弟,那么徒弟的徒子徒孙都不可能再 ...
- oracle----sqlldr用法
SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PAR ...
- 【转】Linux高级字符设备之Poll操作
原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275559.html 在用户程序中,select()和poll()也是与设备阻塞与非阻塞 ...
- jad安装
见:http://www.myexception.cn/eclipse/1469829.html 最开始看了一个坑爹的博客,由于是低级菜鸟一直被误导(设置的是.class的打开方式) 执行完链接够的步 ...
- c# 读取其他程序的ListView内容
ArcMap没找到一个导出图层字段结构的功能,自已花点时间用C#做了个小工具,专门用来导arcmap中图层属性面板中的字段信息. 使用说明: 1) 点击“查找窗口”按钮.2) 在ListView控件上 ...
- 用 Javascript 获取页面大小、窗口大小和滚动条位置
页面大小.窗口大小和滚动条位置这三个数值在不同的浏览器例如 Firefox 和 IE 中有着不同的实现.即使在同一种浏览器例如 IE 中,不同版本也有不同的实现. 本文给出两个能兼容目前所有浏览器的 ...
- Ubuntu 升级到13.10之后出现Apache2启动失败的问题
昨天看到Ubuntu 13.04提示有新的发行版Ubuntu 13.10了,手痒了一下,没有忍住就升级了. 结果升级完毕之后发现Apache2服务启动失败了,失败信息是: Invalid comman ...
- HP-Socket
HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和Agent组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.D ...