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> <!-- 设置常量 --> < ...
随机推荐
- Android 最火的快速开发框架XUtils
参考:http://www.oschina.net/p/xutils 项目git地址https://github.com/wyouflf/xUtils 目录(?)[-] 最近搜了一些框架供初学者学习比 ...
- 总结css兼容问题
目前主流浏览器的兼容性做的都比较好了,本文主要针对IE6,7的不兼容问题进行解决. 1.有浮动存在时,计算一定要精确,不要让内容的宽高超出我们所设置的宽高,IE6下,内容会撑开设置好的高度. 解决方法 ...
- [CTO]创业团队CTO应具备的素质
原文地址:http://www.cyzone.cn/a/20131001/245862_2.html [导读]CTO要让技术团队明白,公司不是请你来搞纯技术研发的,个别人可以纯技术导向,但整个团队的目 ...
- MySQL集群的可行方案
如果单MySQL的优化始终还是顶不住压力时,这个时候我们就必须考虑MySQL的高可用架构(很多同学也爱说成是MySQL集群)了,目前可行的方案有: 一.MySQL Cluster优势:可用性非常高,性 ...
- HiveQL 与 SQL的异同
1 select 别名 (1)别名一定要加as 例:select ID as stuID from students (2) Hive QL不支持在group by, order by 中使用sele ...
- 【英语】Bingo口语笔记(19) - 如何用英语叙旧
- 使用sqlldr将文件中的数据导入到数据库
1.创建数据文件: ?如,在D:\创建 zhaozhenlong.txt 文件,文件内容为: 11,12,1321,22,2331,32,33 2.创建控制文件: 如,在D:\创建 zhaozhenl ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.6. 安装Oracle所依赖的必要包
2.6. 安装Oracle所依赖的必要包 2.6.1. 检查Oracle所依赖的必要rpm包 [root@localhost /]#rpm -q binutils compat-libstdc elf ...
- makefile实例(2)-多个文件实例
1,源文件依赖关系 defs.h command.h buffer.h main.cpp * util.cpp * kde.cpp * * command.cpp * * display.cpp * ...
- 软件测试——boost单元测试 C++
分类: 1. 下载安装Boost 2. 在vs2010 中设置 工具->选项->vc++目录 设置包含文件目录:找到解压的boost文件夹eg:C:\boost_1_43_03. ...