Struts –多个配置文件示例

许多开发人员喜欢将所有与Struts相关的内容(动作,表单)放入单个Struts配置文件中。 对于最初的开发来说速度很快,但是对将来的维护却不利,并且可能是那些开​​发人员不知道Struts允许使用多个配置文件功能。

6年前,我参加了一个大型Struts开发项目,该项目涉及20多个模块。 不幸的是,以前的开发人员将所有与Struts相关的内容(动作,表单等)放入单个Struts配置文件( struts-config.xml )中。 struts-config.xml保持快速增长,最终达到20 ++ mb,对该配置文件的每次更新将花费几分钟,甚至需要半小时才能在Eclipse IDE中进行单个调试部署。 这是一个严重的性能问题,导致项目延误生产日期。 多么好的Struts开发经验。

请将Struts配置详细信息拆分为不同的模块,Struts可以轻松实现。

Struts多个配置文件示例

这是演示的样本项目结构。

Struts-mutiple-configuration-file

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>
测试一下
  1. http:// localhost:8080 / StrutsExample / Page1.do
    它将显示page1.jsp
  2. 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 /

  1. http:// localhost:8080 / StrutsExample / admin / Welcome.do
    它将显示admin / welcome.jsp
  2. http:// localhost:8080 / StrutsExample / common / Welcome.do
    它将显示common / welcome.jsp

每个模块都有自己的Struts配置文件。

下载源代码

下载它– Struts-Mutiple-Config-File-Example.zip

翻译自: https://mkyong.com/struts/struts-multiple-configuration-files-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值