许多开发人员喜欢将所有与Struts相关的内容(动作,表单)放入单个Struts配置文件中。 对于最初的开发来说速度很快,但是对将来的维护却不利,并且可能是那些开发人员不知道Struts允许使用多个配置文件功能。
6年前,我参加了一个大型Struts开发项目,该项目涉及20多个模块。 不幸的是,以前的开发人员将所有与Struts相关的内容(动作,表单等)放入单个Struts配置文件( struts-config.xml )中。 struts-config.xml保持快速增长,最终达到20 ++ mb,对该配置文件的每次更新将花费几分钟,甚至需要半小时才能在Eclipse IDE中进行单个调试部署。 这是一个严重的性能问题,导致项目延误生产日期。 多么好的Struts开发经验。
请将Struts配置详细信息拆分为不同的模块,Struts可以轻松实现。
Struts多个配置文件示例
这是演示的样本项目结构。
1.单模块
一个模块支持多个Struts配置文件。
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>
在web.xml中,可以用逗号“ , ”分隔多个Struts配置文件。
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>
测试一下
- http:// localhost:8080 / StrutsExample / Page1.do
它将显示page1.jsp - http:// localhost:8080 / StrutsExample / common / Welcome.do
它将显示page2.jsp
这两个Struts配置都是load属性。
2.多个模块
多个模块,每个模块都有自己的Struts配置文件。
管理员/welcome.jsp
Welcome to admin page
common / welcome.jsp
Welcome to common page
“ struts-config-admin.xml ”和“ struts-config-admin.xml ”文件都包含相同的设置,Struts能够通过web.xml中的“ config ”参数值来区别它。
在Struts 2中,“ 命名空间 ”是替换此“ 配置参数 ”设置的更有效方法。
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>
测试一下
“ config / admin ”将与此URL模式匹配– http:// localhost:8080 / StrutsExample / admin /
“ config / common ”将与此URL模式匹配– http:// localhost:8080 / StrutsExample / common /
- http:// localhost:8080 / StrutsExample / admin / Welcome.do
它将显示admin / welcome.jsp - http:// localhost:8080 / StrutsExample / common / Welcome.do
它将显示common / welcome.jsp
每个模块都有自己的Struts配置文件。
下载源代码
翻译自: https://mkyong.com/struts/struts-multiple-configuration-files-example/