Struts – Multiple configuration files example
2015-08-28 13:26
495 查看
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 (
Please split the Struts configuration details into different modules, Struts can do it easily.
In the
Test it
It will display the
It will display the
Both Struts configuration are loaded property.
Both “
In Struts 2, the “
The “
Each modules has own Struts configuration file.
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.xmljust 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.
相关文章推荐
- Web Service-- 使用 JDK 发布 WS
- Configure a welcome page in Struts
- java -日期
- java正则表达式练习
- Configure the Struts Tag Libraries
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- eclipse下使用git插件上传代码至github (或下载代码)
- 使用java理解程序逻辑,变量
- Java Socket例程3 UDP
- java状态模式
- Java常用排序
- Struts Hello World Example
- Spring面试题
- Windows平台上,使用eclipse连接到DB2数据库
- 刚刚开始学java,我的第一个比较好一点的Java程序。ps:只要努力,就会有收获
- JAVA正则表达式,matcher.find()和 matcher.matches()的区别
- Java常用的设计模式01:设计模式的分类和原则
- 浅谈Java数据流
- Java之——Timer与ScheduledExecutorService
- JAVA到底是解释型语言还是编译型语言