您的位置:首页 > 编程语言 > Java开发

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 (
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: