Struts – Multiple configuration files example

时间:2021-11-18 07:58:54

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.

Struts – Multiple configuration files example

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的更多相关文章

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

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

  2. Conversion to Dalvik format failed&colon; Unable to execute dex&colon; Multiple dex files define &period;&period;&period;

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  3. 导入项目时,有关&lbrack;2016-04-03 20&colon;38&colon;02 - Dex Loader&rsqb; Unable to execute dex&colon; Multiple dex files 问题

    最近我在学习androidUI设计,在网上找了一个UI菜单界面开源代码示例,按照步骤导入项目,运行的时候控制台结果报了如下错误: [2016-04-03 20:38:02 - Dex Loader] ...

  4. Multiple dex files define Lcom&sol;google&sol;zxing&sol;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&colon; Multiple dex files define

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

  6. TN035&colon; Using Multiple Resource Files and Header Files with Visual C&plus;&plus;

    TN035: Using Multiple Resource Files and Header Files with Visual C++ This note describes how the Vi ...

  7. Unable to execute dex&colon; Multiple dex files define Lcom&sol;gl

    [2015-04-16 17:42:04 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/gl/softphon ...

  8. &OpenCurlyDoubleQuote;Unable to execute dex&colon; 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. angular中的MVVM模式

    在开始介绍angular原理之前,我们有必要先了解下mvvm模式在angular中运用.虽然在angular社区一直将angular统称为前端MVC框架,同时angular团队也称它为MVW(What ...

  2. Applying Eigenvalues to the Fibonacci Problem

    http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...

  3. poj1799---解析几何

    sin(a)=r/R-r,反三角asin(r/R-r),乘以2n=2pi,去化简,得到r 收获:define pi acos(-1) 这样pi的精度会高很多<math.h>(cos,sin ...

  4. 从一个简单案例上手Spring MVC,同时分析Spring MVC面试问题

    很多公司都会用Spring MVC,而且初级程序员在面试时,一定会被问到这方面的问题,所以这里我们来通过一个简单的案例来分析Spring MVC,事实上,我们在培训中就用这个举例,很多零基础的程序员能 ...

  5. 利用Java手写简单的httpserver

    前言: 在看完尚学堂JAVA300中讲解如何实现一个最简单的httpserver部分的视频之后, 一.前置知识 1.HTTP协议 当前互联网网页访问主要采用了B/S的模式,既一个浏览器,一个服务器,浏 ...

  6. Linux - awk 文本处理工具二

    awk 判断格式 awk '{print ($1>$2)?"第一排"$1:"第二排"$2}' # 条件判断 括号代表if语句判断 "?&quot ...

  7. 【总结】Java面试题

    部分转自 https://blog.csdn.net/junchi_/article/details/79754032 一.String特性.StringBuffer 和 StringBuilder ...

  8. 【Android】Android Theme的设置

    在AndroidManifest.xml文件中,可以对每一个Activity设置android:theme theme的设置 可以设置为系统自带的格式,也可以自定义格式. A: 系统自带格式 @and ...

  9. &period;NET基础 (18)特性

    特性1 什么是特性,如何自定义一个特性2 .NET中特性可以在哪些元素上使用3 有哪几种方法可以获知一个元素是否申明某个特性4 一个元素是否可以重复申明同一个特性 特性1 什么是特性,如何自定义一个特 ...

  10. 云计算之路-试用Azure:拐弯抹角的负载均衡

    负载均衡是云服务商标配的免费服务.阿里云的对应产品叫SLB,UCloud的对应产品叫ULB,操作都很简单直观,进入负载均衡管理控制台,添加负载均衡,选择相应的虚拟机即可. 而Azure则完全不一样,在 ...