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

  1. This is Page 1

page2.jsp

  1. This is Page 2

struts-config-1.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts-config PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  4. "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
  5. <struts-config>
  6. <action-mappings>
  7. <action
  8. path="/Page1"
  9. type="org.apache.struts.actions.ForwardAction"
  10. parameter="/pages/page1.jsp"/>
  11. </action-mappings>
  12. </struts-config>

struts-config-2.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts-config PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  4. "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
  5. <struts-config>
  6. <action-mappings>
  7. <action
  8. path="/Page2"
  9. type="org.apache.struts.actions.ForwardAction"
  10. parameter="/pages/page2.jsp"/>
  11. </action-mappings>
  12. </struts-config>

In the web.xml, you can separate multiple Struts configure file by a comma “,“.

web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Maven Struts Examples</display-name>
  6. <servlet>
  7. <servlet-name>action</servlet-name>
  8. <servlet-class>
  9. org.apache.struts.action.ActionServlet
  10. </servlet-class>
  11. <init-param>
  12. <param-name>config</param-name>
  13. <param-value>
  14. /WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
  15. </param-value>
  16. </init-param>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>action</servlet-name>
  21. <url-pattern>*.do</url-pattern>
  22. </servlet-mapping>
  23. </web-app>

Test it

  1. http://localhost:8080/StrutsExample/Page1.do

It will display the page1.jsp

  1. 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

  1. Welcome to admin page

common/welcome.jsp

  1. 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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts-config PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  4. "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
  5. <struts-config>
  6. <action-mappings>
  7. <action
  8. path="/Welcome"
  9. type="org.apache.struts.actions.ForwardAction"
  10. parameter="/welcome.jsp"/>
  11. </action-mappings>
  12. </struts-config>

web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Maven Struts Examples</display-name>
  6. <servlet>
  7. <servlet-name>action</servlet-name>
  8. <servlet-class>
  9. org.apache.struts.action.ActionServlet
  10. </servlet-class>
  11. <init-param>
  12. <param-name>config</param-name>
  13. <param-value>
  14. /WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
  15. </param-value>
  16. </init-param>
  17. <init-param>
  18. <param-name>config/admin</param-name>
  19. <param-value>
  20. /WEB-INF/struts-config-admin.xml
  21. </param-value>
  22. </init-param>
  23. <init-param>
  24. <param-name>config/common</param-name>
  25. <param-value>
  26. /WEB-INF/struts-config-common.xml
  27. </param-value>
  28. </init-param>
  29. <load-on-startup>1</load-on-startup>
  30. </servlet>
  31. <servlet-mapping>
  32. <servlet-name>action</servlet-name>
  33. <url-pattern>*.do</url-pattern>
  34. </servlet-mapping>
  35. </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/

  1. http://localhost:8080/StrutsExample/admin/Welcome.do
  2. It will display the admin/welcome.jsp
  3. http://localhost:8080/StrutsExample/common/Welcome.do
  4. It will display the common/welcome.jsp

Each modules has own Struts configuration file.

Struts – Multiple configuration files example的更多相关文章

  1. react 配置ant时遇见的一个Error: Multiple configuration files found. Please remove one: – package.json#babel – .babelrc 解决方案

    这个问题是create react app 里面的package.json里面已经配置了   "babel": {     "presets": [       ...

  2. 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 ... 这个错误是因为有两个相 ...

  3. 导入项目时,有关[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] ...

  4. 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 ...

  5. 用Eclipse运行Android版APP(PhoneGap)时出现:Unable to execute dex: Multiple dex files define

    这两天遇到点小问题,做个记录: 症状:运行,调试时都报:Unable to execute dex: Multiple dex files define错误,发布后的APP安装到手机后一运行,就提示: ...

  6. 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 ...

  7. 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 ...

  8. “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 ...

  9. Multiple dex files define

    Multiple dex files define 在项目中,有一个类的包名和引用的jar包中的类和包名一致

随机推荐

  1. sql server 2008r2 清除数据库日志

    USE [master] GO ALTER DATABASE data SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE data  SET REC ...

  2. Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数

    library('ggplot2') prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PC ...

  3. Qt之QHeaderView添加复选框

    简述 前面分享了QTableView中如何添加复选框.本节主要介绍QTableView中的表头-QHeaderView添加复选框的功能,下面以水平表头为例,垂直表头类似! 简述 效果 QHeaderV ...

  4. UVa 455 Periodic Strings

    题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. #include<iostream> #include<cstdio> #include< ...

  5. [转载]charisma-master 加载慢的原因及解决方法

    [我的总结] 原文中指出的地址有的已经转换,因为版本问题. 所以根据2014年11月获取的charisma-master版本,应做以下更改: 1.charisma-app.css 这个文件中的外链字体 ...

  6. iPad中控制器view的width和height

    一.iPad中控制器view的width和height 1> 规律 * width 是宽高中最小的那个值 * height 是宽高中最大的那个值 2> 举例(比如窗口根控制器的view,有 ...

  7. A*寻路初探 GameDev.net 转载

    A*寻路初探 GameDev.net 译者序:很久以前就知道了A*算法,但是从未认真读过相关的文章,也没有看过代码,只是脑子里有个模糊的概念.这次决定从头开始,研究一下这个被人推崇备至的简单方法,作为 ...

  8. acdream 1683 村民的怪癖(KMP,经典变形)

    Problem Description 娜娜费劲九牛二虎之力终于把糖果吃完了(说好的吃不完呢?骗人,口亨~),于是,缘溪行,忘路之远近.忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷,娜娜甚异之 ...

  9. SkinPP for VC

    1.下载文件:SkinPPWTL.h,SkinPPWTL.dll,SkinPPWTL.lib以及Skin++皮肤库: 2.新建一个工程,如:基于多文档的工程,名为:MySkin: 3.将下载的Skin ...

  10. 内存泄露(OOM)现象及举例

    一.HeapSize OOM(堆空间内存溢出) A.eg:List.add(" ")在一个死循环中不断的调用add却没有remove. B.并发导致. 解决方法有:1.代码提速.这 ...