需求
需要将一个模块分割成多个子模块,每个模块有自己的配置文件。
解决方案
在web.xml中配置如下:
<!-- Action Servlet Configuration -->
<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.xml</param-value>
</init-param>
<init-param>
<param-name>config/module1</param-name>
<param-value>/WEB-INF/struts-config-module1.xml</param-value>
</init-param>
<init-param>
<param-name>config/module2</param-name>
<param-value>/WEB-INF/struts-config-module2.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
讨论
struts引入模块的概念将一个应用程序分割成多个具有不同功能的子模块。每个模块可以定义自己的配置文件。简单的struts程序隐含一个默认的模块,它没有子模块名。添加的子模块用一个前缀config/来定义。如上例中定义了三个模块。第一个init-param定义了一个默认模块。第二、三个init-param分别定义了module1,module2两个模块。
struts通过在web.xml中对模块的声明处理每一个请求的模块信息。它将作用于global forwards, global exceptions, action mappings, local forwards, and local exceptions的path属性。各个模块的配置文件struts-config.xml不需要知道自己属于哪个模块
如果你使用struts 标签如html:link和html:rewrite生成URL,则URL中将包含模块的名称。如:
<html:link page="/validateswitch.do?prefix=/validate&page=/input.jsp">to validate/input.jsp</html:link>
一般情况下,应用程序会将所有的图片文件放在一个顶层目录中(如:<top-level>/images),
如果你使用模块时,当你用到标签html:img时需要为每个模块建立一个images文件夹,或设置在html:img标签的module属性为空字符串(“”)指明images文件假在应用程序根目录下。
评论