先来看一下上一篇博客中工程的配置文件:
这个文件中有两个包,也可以说是有两个模块。当一个工程比较大的时候,里面的模块可能会有几十甚至上百个,如果都这样写着struts.xml文件中,那么struts.xml难免就会非常的混乱。struts2提供了分模块配置方法。通过一个示例来看一下:
1.新建工程SecondLearnStruts2Chap02_04:假定这个工程下有两个大模块分别是车辆管理和资产管理。
2.在src目录下新建cheliang.xml文件,作为车辆管理模块的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="cheliang" namespace="/cheliang" extends="struts-default">
<action name="cheliang" class="com.test.action.CheLiangAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
</struts>
新建zichan.xml作为资产管理模块的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="zichan" namespace="/zichan" extends="struts-default">
<action name="zichan" class="com.test.action.ZiChanAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
</struts>
通过<include file="" ></include>标签把这两个配置文件包含在struts.xml文件中:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="cheliang.xml"></include>
<include file="zichan.xml"></include>
</struts>
这样我们就把车辆管理和资产管理两个模块的配置文件都配置到了struts.xml文件中。
3.新建CheLiangAction:
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class CheLiangAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("执行了CheLiangAction的默认方法");
return SUCCESS;
}
}
新建ZiChanAction:
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class ZiChanAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("执行了ZiChanAction的默认方法");
return SUCCESS;
}
}
运行程序:
控制台输出为:
程序运行成功了。
总结一下:这里通过<include file="" ></include>实现struts2工程的分模块配置。