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> <!-- 设置常量 --> < ...
随机推荐
- 函数lock_rec_get_n_bits
/*********************************************************************//** Gets the number of bits i ...
- ASP.NET MVC+EasyUI+Entity FrameWork 整合开发
本文详细讲解怎么用ASP.NET MVC+EasyUI+Entity FrameWork 来开发一个项目 对于ASP.NET MVC的Jscript库,主要引用 <script type=.mi ...
- BZOJ 4269 再见Xor
线性基. 求次大值就是再异或一个线性基好了. #include<iostream> #include<cstdio> #include<cstring> #defi ...
- IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度
一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...
- zoj 3690 Choosing number
题意 就是说给你 N 个人站成一排,现在每个人都可以选择 1-M 中间的任意一个数字,相邻的两个人数字相同,则他必须是是 > K 的 问方案总数: 方法 先求出递推式,然后用矩阵 ...
- Linux多线程下载工具Axel
一般情况,在 Linux 下我们习惯于用 wget 下载,但该工具的缺点就是无法进行多线程下载,所以往往有时候速度不够快.这里介绍的 Axel,是 Linux 下一款不错的 HTTP 或 FTP 高速 ...
- IconRes提供免费高质量的Material风格android官方图标库
连接地址: http://www.iconres.com/android/index.php
- jsonp调用实例
首先我们来定义接口的规范,就像这样:http://www.mydomain.com/jsonp/ticker?symbol=IBM&callback=showPricesymbol是请求条件, ...
- Android开发如何在4.0及以上系统中自定义TitleBar
本文将通过一个实例讲解怎么实现在4.0及以上系统版本中实现自定义TitleBar,这只是我自己找到的一种方法; xml布局文件 activity_main.xml <RelativeLayout ...
- go 应用程序性能测试
runtime/pprof 我们要加入对pprof包里的方法调用,程序才能将运行时候程序的堆内存分配状态记录到文件(也可以是写到其他地方,例如网络等)中,以便进一步的分析. 如果你的go程序只是一个应 ...