`
skyj129
  • 浏览: 12908 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

struts中配置多模块

阅读更多

  同一个应用包含多个子应用,每一个字应用处理相关的一组功能,所有的子应用共用同一个actionservlet,但是每一个子应用都是单独的配置文件,把应用划分为多个模块的步骤:

     1.为每一个子应用创建一个单独的struts的配置文件

     2.在web.xml文件中的actionservlet的配置信息中添加每一个子应用的信息

     3.采用forward元素和switchaction类来实现子应用之间的切换

  可以将一个struts-config.xml分割成多个小的struts-config.xml。每个都必须按照struts-config DTD文件的格式进行编写。配置举例如下:
<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,
      /WEB-INF/struts-config-2.xml
    </param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
当载入ActionServlet类时,struts将多个配置文件拼成单个的配置文件。
如果多个配置文件中有重复的元素则使用最后一个配置文件中的。
你也可以为多个字模块指定多个配置文件。如下所示:
<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-default-config.xml,
      /WEB-INF/struts-default-config-2.xml
    </param-value>
 </init-param>
 <init-param>
    <param-name>config/module1</param-name>
    <param-value>
      /WEB-INF/struts-module1-config.xml
    </param-value>
 </init-param>
 <init-param>
    <param-name>config/module2</param-name>
    <param-value>
      /WEB-INF/struts-module2-config.xml,
      /WEB-INF/struts-module2-config-2.xml,
      /WEB-INF/struts-module2-config-3.xml
    </param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
模块化
需求
需要将一个模块分割成多个子模块,每个模块有自己的配置文件。
解决方案
在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文件假在应用程序根目录下。
模块间的切换
问题
想从一个模块(源模块)切换到另一个模块(目标模块)。
解决方案
在你的源模块中建立一个type为“org.apache.struts.actions.SwitchAction”的action:
<action path="/ChangeModuleTest"
        type="org.apache.struts.actions.SwitchAction"/>
使用这个action,向它传递两个参数prefix和page。prefix指明了目标模块名称,必须是以斜扛开头(“/”),page指明了相对于目标模块的位置。如:
<html:link page="/ChangeModuleTest.do?prefix=moduleName&page=/SomeAction.do">
    Change Module
</html:link>如果你要连接到一个action,别忘了加上”.do”
讨论
SwitchAction能将应用程序从一个模块转到另一个模块,然后跳转到目标模块的指定位置。
同ForwardAction、IncludeAction一样SwitchAction不需要子类化。
 
当我们在浏览器中使用http://hostaddress/contextpath/module/action.do式样的的url时,actionservlet会根据module选择模块对象,下面是actionservlet处理http请求的代码: 
  protected void process(HttpServletRequest request,
                           HttpServletResponse response)
        throws IOException, ServletException {
        RequestUtils.selectModule(request, getServletContext());
               getRequestProcessor(getModuleConfig(request)).process
            (request, response);
    }


protected void process(HttpServletRequest request,
                           HttpServletResponse response)
        throws IOException, ServletException {
        RequestUtils.selectModule(request, getServletContext());
               getRequestProcessor(getModuleConfig(request)).process
            (request, response);
    }


RequestUtils.selectModule函数将使用下面的代码把url中的模块前缀(下面代码的prefix将代表上面url式样中的/module)指定的模块对象保存在request属性中,这个模块对象就成了处理这个请求的当前模块对象:

 //   Expose the resources for this module
        ModuleConfig config = (ModuleConfig)
 context.getAttribute(Globals.MODULE_KEY + prefix);

 if (config != null) {
            request.setAttribute(Globals.MODULE_KEY, config);
        }
 else {
            request.removeAttribute(Globals.MODULE_KEY);
        }
 

x);
        if (config != null) {
            request.setAttribute(Globals.MODULE_KEY, config);
        }
 else {
            request.removeAttribute(Globals.MODULE_KEY);
        }


 
也许你会认为从一个模块跳转到另一个模块可以在一个link或action的URL中使用模块前缀。例如,你已经创建了一个模块admin,你想创建一个从默认模块链接到你的admin模块的主页。你也许会象下面这样写:
 
<html:link action="/admin/Main.do">Admin Module</html:link>
你会发现着段代码不起作用。

   

既然你不能直接链接到另一个模块的JSP页面,那么你需要在在你的源模块中建立SwitchAction,引用链接路径,传递路径中的prefix和page请求参数。
<html:link page="/SwitchModule.do?prefix=admin&page=/Main.do">
    Admin Module
</html:link>
SwitchAction将通过prefix和page参数指定的信息进行模块跳转。参数信息如下: 



要跳转到另一个模块的一个action,请求必须经过控制层,如果你将一个请求直接发送向另一个模块的某个JSP,则struts将不能访问那个模块指定的struts 对象,如资源束,plug-in还有global forwards。SwitchAction可以保证你的请求经过控制层。

既然你不能直接链接到另一个模块的JSP页面,那么你需要在在你的源模块中建立SwitchAction,引用链接路径,传递路径中的prefix和page请求参数。
<html:link page="/SwitchModule.do?prefix=admin&page=/Main.do">
    Admin Module
</html:link>
SwitchAction将通过prefix和page参数指定的信息进行模块跳转。参数信息如下: 


// Expose the resources for this module
        ModuleConfig config = (ModuleConfig)
 context.getAttribute(Globals.MODULE_KEY + prefi
分享到:
评论

相关推荐

    struts1多模块多配置文件

    struts1多模块多配置文件的开发流畅图解

    struts核心配置文件详解

    Struts应用的配置 多应用模块的配置 Struts配置文件 多应用模块的划分有助于应用的并行开发,提高效率

    struts核心配置资源文件

    Struts应用的配置 多应用模块的配置 Struts配置文件 多应用模块的划分有助于应用的并行开发,提高效率。

    struts多模块

    struts多模块相关配置的介绍

    struts的教程.doc

    Struts配置文件简介 13 有关Struts Controller及其相关的的配置描述 13 有关struts tag lib的配置描述 14 有关Struts Action Mapping的配置描述 14 Form-bean元素 15 Action元素 15 Struts高级特性(Struts ...

    Struts 之旅 - 配置多应用模块

    与我博客里面的同名文章是对应的

    Struts2全解Struts2全解

    Namespace、自定义Action、路径问题、通配符、包含模块配置文件、默认Action、接受用户输入、服务器跳转、Action中访问web元素、简单数据校验、调用Action的自定义方法 5struts2国际化 ......... 6 struts2输入校验...

    java Struts教程

    Struts配置文件简介 13 有关Struts Controller及其相关的的配置描述 13 有关struts tag lib的配置描述 14 有关Struts Action Mapping的配置描述 14 Form-bean元素 15 Action元素 15 Struts高级特性(Struts Advanced...

    web页面模块化异步渲染struts-gpipe.zip

    }这里需要配置模块的名字,渲染方式:同步还是异步,返回的ftl路径。模块的名字和主模块里面名字对应:sync: ${GPipe_m1} anyn: ${GPipe_m2}其中GPipe_是我给模块名字起的前缀,这个固定,后面的对应groovy脚本...

    深入浅出struts2

    为了达成这一目标,Struts2中提供了很多新特性,比如智能的默认设置、annotation的使用以及“惯例重于配置”原则的应用,而这一切都大大减少了XML配置。Struts2中的Action都是POJO,这一方面增强了Action本身的可...

    谈谈你对Struts的理解。

     一个扩展知识点:struts的配置文件可以有多个,可以按模块配置各自的配置文件,这样可以防止配置文件的过度膨胀;  2. ActionServlet把请求交给action去处理之前,会将请求参数封装成一个formbean对象(就是一个...

    Struts2详细工作流程

    Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher、业务控制器Action和用户实现的企业业务逻辑组件...Struts 2框架按照模块来划分,可以分为Servlet Filters、Struts核心模块、拦截器和用户实现部分。

    struts 教程 很好很详细

    Struts配置文件简介 13 有关Struts Controller及其相关的的配置描述 13 有关struts tag lib的配置描述 14 有关Struts Action Mapping的配置描述 14 Form-bean元素 15 Action元素 15 Struts高级特性(Struts Advanced...

    jfreechar 整合struts2.1.8版本生成线图,饼图,柱形图

    -- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 --&gt; &lt;include file="struts-default.xml"&gt; &lt;!-- package提供了将多个Action组织为一个模块的方式 ...

    spring2.5 struts2.0 hibernate3.2.5 搭建的企业级开发基础模块

    该项目是根据朋友的ERP中对仓库管理的设计,基础模块(用户、菜单、角色、职务)都已完成,业务模块有数据库设计文档。 SSH框架的配置文件分别是 Spring:spring.service.cfg.xml Hibernate:spring.local....

    Java进阶:Struts多模块的技巧

    在使用struts多模块的,找到一些小技巧和经验,与大家分享一下。 关于多module的配置就不说了,只需要用不同的config, struts-config.xml作为默认module, struts-config-module.xml作为/module的配置  CSDN上有...

    struts2.0.jar

    Struts 2.0框架中出现的许多特性旨在让Struts更容易使用: · 改进的设计: 与Struts 1相比,...· 易于定制的控制器: Struts 1允许请求处理程序可按照模块来定制,在Struts 2中,需要的话,可以按照动作来定制请求处理

    Struts2 学习笔记

    十三、 Struts2配置文件模块化包含(include) 17 十四、 默认的Action 18 十五、 Action总结 18 02 Struts2-Result 19 一、 Result类型 (type) 19 二、 全局结果集(Globle Result) 20 三、 动态的结果集(dynamic ...

    struts 入门教程

    Struts配置文件简介 13 有关Struts Controller及其相关的的配置描述 13 有关struts tag lib的配置描述 14 有关Struts Action Mapping的配置描述 14 Form-bean元素 15 Action元素 15 Struts高级特性(Struts Advanced...

Global site tag (gtag.js) - Google Analytics