Struts – Multiple configuration files example
Many developers like to put all Struts related stuff (action, form) into a single Struts configuration file. It’s fast for the initial development but bad for the future maintenance, and may be those developers are not aware of the Struts is allow multiple configuration files feature.
6 years ago, I had joined a large Struts development project which involve 20+ modules. Unfortunately, the prior developers put all the Struts related stuff (action, form and etc) into a single Struts configuration file (struts-config.xml
). The struts-config.xml
just keep growing extremely fast and finally hit 20++mb, every update to this configuration file will take few minutes, and even wait half an hour for a single debugging deployment in Eclipse IDE. This is a serious performance issue and causing the project keep delay the production date. What a good Struts development experience.
Please split the Struts configuration details into different modules, Struts can do it easily.
Struts multiple configuration files example
This is the sample project structure for the demonstration.
1. Single module
A single module support multiple Struts configuration files.
page1.jsp
This is Page 1
page2.jsp
This is Page 2
struts-config-1.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="/Page1"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/page1.jsp"/>
</action-mappings>
</struts-config>
struts-config-2.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="/Page2"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/page2.jsp"/>
</action-mappings>
</struts-config>
In the web.xml
, you can separate multiple Struts configure file by a comma “,
“.
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Maven Struts Examples</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Test it
http://localhost:8080/StrutsExample/Page1.do
It will display the page1.jsp
http://localhost:8080/StrutsExample/common/Welcome.do
It will display the page2.jsp
Both Struts configuration are loaded property.
2. Multiple modules
Multiple modules, each has own Struts configuration files.
admin/welcome.jsp
Welcome to admin page
common/welcome.jsp
Welcome to common page
Both “struts-config-admin.xml
” and “struts-config-admin.xml
” files contains the same settings, Struts is able to differential it via the “config
” parameter value in web.xml
.
In Struts 2, the “Namespace
” is a more efficient way to replace this “config parameter” setting.
struts-config-admin.xml, struts-config-admin.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="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/welcome.jsp"/>
</action-mappings>
</struts-config>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Maven Struts Examples</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
</param-value>
</init-param>
<init-param>
<param-name>config/admin</param-name>
<param-value>
/WEB-INF/struts-config-admin.xml
</param-value>
</init-param>
<init-param>
<param-name>config/common</param-name>
<param-value>
/WEB-INF/struts-config-common.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Test it
The “config/admin
” will match to this URL pattern – http://localhost:8080/StrutsExample/admin/
The “config/common
” will match to this URL pattern – http://localhost:8080/StrutsExample/common/
http://localhost:8080/StrutsExample/admin/Welcome.do
It will display the admin/welcome.jsp
http://localhost:8080/StrutsExample/common/Welcome.do
It will display the common/welcome.jsp
Each modules has own Struts configuration file.
Struts – Multiple configuration files example的更多相关文章
- react 配置ant时遇见的一个Error: Multiple configuration files found. Please remove one: – package.json#babel – .babelrc 解决方案
这个问题是create react app 里面的package.json里面已经配置了 "babel": { "presets": [ ...
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- 导入项目时,有关[2016-04-03 20:38:02 - Dex Loader] Unable to execute dex: Multiple dex files 问题
最近我在学习androidUI设计,在网上找了一个UI菜单界面开源代码示例,按照步骤导入项目,运行的时候控制台结果报了如下错误: [2016-04-03 20:38:02 - Dex Loader] ...
- Multiple dex files define Lcom/google/zxing/BarcodeFormat
解决zxing “Could not find class 'com.goole.zxing.Result”和“Multiple dex files define”问题 时间 2014-04-24 1 ...
- 用Eclipse运行Android版APP(PhoneGap)时出现:Unable to execute dex: Multiple dex files define
这两天遇到点小问题,做个记录: 症状:运行,调试时都报:Unable to execute dex: Multiple dex files define错误,发布后的APP安装到手机后一运行,就提示: ...
- TN035: Using Multiple Resource Files and Header Files with Visual C++
TN035: Using Multiple Resource Files and Header Files with Visual C++ This note describes how the Vi ...
- Unable to execute dex: Multiple dex files define Lcom/gl
[2015-04-16 17:42:04 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/gl/softphon ...
- “Unable to execute dex: Multiple dex files”如何解决?
遇到报错: [2014-02-13 17:27:03 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/kkdia ...
- Multiple dex files define
Multiple dex files define 在项目中,有一个类的包名和引用的jar包中的类和包名一致
随机推荐
- android利用数字证书对程序签名
签名的必要性 1. 防止你已安装的应用被恶意的第三方覆盖或替换掉. 2. 开发者的身份标识,签名可以防止抵赖等事件的发生. 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同 ...
- QQ发送邮件
//下面开始写SendEmail函数 public void SendEmail(string Emailshoujian, string Emailbiaoti, string Emailzheng ...
- JProfiler 使用说明
一个全功能的 Java 剖析工具( profiler ),专用于分析 J2SE 和 J2EE 应用程序.它把 CPU .执行绪和内存的剖析组合在一个强大的 应用中. JProfiler 可提供许多 I ...
- php延迟加载的示例
class a{ ; ; //public $d = 5; public function aa(){ echo self::$b; } public function cc(){ echo stat ...
- HDU1010 Tempter of the Bone
解题思路:相当经典的一题,回溯,具体细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> ...
- aspose输出表格
利用aspose在word中输出表格 序号 姓名 性别 <<TableStart:T>><<Index>> <<Name>> ...
- 【转】安装Django
原文网址:http://www.crifan.com/record_install_django/ 1.参考Quick install guide,最终找到下载的地址: http://bitnami. ...
- Arduino运行时突然[卡死在某一行/立即重启/串口输出乱码/程序执行不正常]的可能原因
1.这一行是分配内存,而内存不够了(Arduino uno只有2k) 2.内存本身已经只剩一点点了,于是就有莫名其妙的问题 3.没有调用Wire.begin().xx.setup()之类的操作!
- Android服务之Service
android中服务是运行在后台的东西,级别与activity差不多.既然说service是运行在后台的服务,那么它就是不可见的,没有界面的东西.你可以启动一个服务Service来播放音乐,或者记录你 ...
- java 学习资料
最近一直在从java基础开始学习java,感觉个人学习思路还是比较混乱的. 看到Java工程师成神之路中介绍了需要学习的知识点,并在<成神之路系列文章>中分章分节的开始介绍其中所有的知识点 ...