`
hdxiong
  • 浏览: 371822 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts.xml 模块化管理

    博客分类:
  • SSH
阅读更多
先贴两段代码,在慢慢解释

(1)struts-user.xml
<struts> 
    <package name="struts-user" extends="struts-default"> 
        <global-results> 
            <result type="redirect-action">UserAction_queryAll</result> 
        </global-results> 
        <action name="UserAction_login" class="userAction" method="login"></action> 
        <action name="UserAction_insert" class="userAction" method="insert"></action> 
        <action name="UserAction_update" class="userAction" method="update"></action> 
        <action name="UserAction_delete" class="userAction" method="delete"></action> 
        <action name="UserAction_queryById" class="userAction" method="queryById"></action>  
        <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action> 
        <action name="UserAction_queryAll" class="userAction" method="queryAll"> 
            <result>/user/user_list.jsp</result> 
        </action> 
          </package> 
</struts>


(2)struts.xml(引入了struts-user.xml)
<struts> 
    <include file="struts-user.xml"></include> 
    <package name="struts" extends="struts-default"></package> 
</struts>

1. 使用<include>标签重用配置文件

(1)在Struts2中提供了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签引用这些配置文件。如上面的代码。

注意:用<include>引用的xml文件也必须是完成的struts2的配置。实际上<include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。

注意:struts.xml和struts-user.xml中<package></package>标签中的name属性不能相同。道理很简单,<struts></struts>标签中可以有多个<package></package>标签,要通过name属性以示区别。

(2)Apache Struts 2 Documentation: Can we break up a large struts.xml file into smaller pieces --> Yes, there are two approaches. We can include other struts.xml file from a bootstrap, or we can package a struts.xml files in a JAR. Or both.

<1>By Include:A typical struts.xml files will have one or more include elements:

<struts>
    <include file="struts-default.xml"/>
    <include file="config-browser.xml"/>
    <package name="default" extends="struts-default">
    ....
    </package>
    <include file="other.xml"/>
</struts>
The first include element tells the framework to load the struts-default.xml, which it will find in the struts2.jar file. The struts-default.xml file defines the "standard" interceptor and result definitions. You can put your own <include> elements in your struts.xml interchangeably with <package> elements. The configuration objects will be loaded in the order of appearance. The framework reads the configuration from top to bottom and adds objects as they are referenced.

<2>By JAR

A "module" can be added to an application by placing a struts.xml and related classes into a JAR on the classpath. FreeMarker and Velocity templates can also be provided by JAR, making it possible to distribution a module in a single, self-contained JAR that is automatically configured on startup.

2. 全局result(global-results)

(1)有很多时候一个<result>可供很多<action>使用,这时可以使用<global-results>标签来定义全局的<result>,代码见struts-user.xml。执行顺序:当一个Action返回的String没有相应的<result>与之对应,Struts2就会查找全局的<result>。

(2)Apache Struts 2 Documentation: Global Results

Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result. If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.

<!-- Defining global results -->
<global-results>
    <result name="error">/Error.jsp</result>
    <result name="invalid.token">/Error.jsp</result>
    <result name="login" type="redirectAction">Logon!input</result>
</global-results>
3. <include>标签和<global-results>标签结合

(1)<global-results>标签的作用域只是当前<struts></struts>,也可以说是当前的xml文件;struts2不允许把struts-user.xml(通过<include>引入到struts.xml)中的<global-results>标签写在struts.xml中。
(2)如果struts-user.xml中的package继承自struts.xml中的package,则可以将struts-user.xml中的<global-results>放在struts.xml中。然后struts-user.xml将此<global-results>从struts.xml中继承过来。例如(将上面的两段代码简单修改):

(1)struts-user.xml
<struts> 
    <!-- 这里struts-user继承(extends)的是struts, 即struts.xml中package的name属性值 --> 
    <package name="struts-user" extends="struts"> 
         
        <action name="UserAction_login" class="userAction" method="login"></action> 
        <action name="UserAction_insert" class="userAction" method="insert"></action> 
        <action name="UserAction_update" class="userAction" method="update"></action> 
        <action name="UserAction_delete" class="userAction" method="delete"></action> 
        <action name="UserAction_queryById" class="userAction" method="queryById"></action>  
        <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action> 
        <action name="UserAction_queryAll" class="userAction" method="queryAll"> 
            <result>/user/user_list.jsp</result> 
        </action> 
    </package> 
</struts>

(2)struts.xml(引入了struts-user.xml)
<struts> 
    <include file="struts-user.xml"></include> 
    <package name="struts" extends="struts-default"> 
        <global-results> 
            <result type="redirect-action">UserAction_queryAll</result> 
        </global-results> 
    </package> 
</struts>
分享到:
评论

相关推荐

    Struts_config.xml详解

    (相当于初始化赋值) &gt;&gt;&gt; prefix=""// 指定填充当前 Action 关联 FormBean 时 ,要添加到请求参数名称的前缀,因此,如果请求参数名为 "username" 并且 prefix 属性被设置为 "search" , 则将对 FormBean 调用一个...

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

    struts-gpipe 提供了将 groovy 引入 struts java web 项目的功能, web页面模块化异步渲染。 这个项目的初衷是为了将groovy引入我们的struts web项目,在开发的过程中,发现有跟多可以做的是全,不过在最初的版本,...

    J2EE电子商务系统开发从入门到精通

    5.2 数据库设计及持久化..... 79 5.2.1 逻辑视图..... 79 5.2.2 数据库建表..... 79 5.2.3 Hibernate配置文件基本配置..... 80 5.2.4 封装Hibernate数据库操作方法..... 81 5.2.5 数据持久化处理..... 84 5.2.6 ...

    Struts in Action中文版

    2.6.2. Struts的强项........................................................................................................58 Struts in Action 中文版 Lastest Revised:10/14/2005 10:27:00 AM ...

    SSH开发纪要整合解决四大问题(中文、jar包冲突、延时加载、模块化)文档

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"&gt; &lt;struts-...

    OA办公自动化管理系统(Struts1.2+Hibernate3.0+Spring2+DWR)130224.rar

    OA办公自动化管理系统是一个基于Struts1.2、Hibernate3.0、Spring2和DWR技术实现的Java Web应用项目。该系统采用了MVC设计模式,将业务逻辑、数据访问和表示层分离,提高了代码的可维护性和可扩展性。Struts1.2作为...

    struts in Action

    1.1.1. 谁创建了Struts?...................................................................................................19 1.1.2. 为什么Struts 要开源?....................................................

    struts2.1宝典

    9.将struts.xml分开 4 10全局结果result 4 11模型驱动 4 12.创建拦截器 5 13表单提交中文乱码问题 5 14.Jsp不能识别EL 5 15.自定义标签中访问Dao 获取dao 6 16.标签获取参数 6 17.Action之间传递错误验证信息 6 18第...

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

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

    基于SSH模拟当当网项目(电子商务平台)

    web.xml需要定义ContextLoaderListener,实例化容器配置 5.将事务管理交个Spring,采用AOP方式,删除原有Struts事务拦截器 -------------改造步骤----------------- 例如用户注册功能 1) 引入Hibernate开发包 2) ...

    JAVA期末大作业课程设计基于SSH框架的管理系统.zip

    Struts框架的总控制器ActionServlet是一个Servlet,在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(struts-config.xml)的配置信息,为Struts中不同的模块初始化相应的对象。(面向对象思想) ...

    Struts原理、开发及项目实施

    另外,Struts还提供了一系统实用对象:XML处理、通过Java reflection APIs自动处理JavaBeans属性、国际化的提示和消息等。 &lt;br/&gt; 7、一个实例 &lt;br/&gt; 一个用户注册系统,用户通过网页输入相关信息...

    DWR,Struts,Hibernate和Spring的J2EE架构开发大全

    基于SSH构架的MIS用户管理模块的实现.pdf 基于SSH的播客资源平台的设计与实现.pdf 基于SSH的本科教学评估辅助系统的设计与实现.pdf 基于SSH的物流信息系统的研究与实现.pdf 基于SSH的高校学生管理系统设计与实现.pdf...

    struts2.4+spring3.1+hibernate4.1的SSH框架

     系统的基本业务流程是: 在表示层中,首先通过JSP页面实现交互界面,负责传送请求(Request)和接收响应(Response),然后Struts根据配置文件(struts-config.xml)将ActionServlet接收到的Request委派给相应的Action...

    深入浅出struts2

    开发人员还可以通过拦截器(可以自定义拦截器或者使用Struts2提供的拦截器)来对请求进行预处理和后处理,这样一来,处理请求就变得更加模块化,从而进一步减小耦合度。模块化是一个通用的主题——可以通过插件机制...

    WABACUS整合Struts2、DWZ的项目源码

    修改简单改造了WABACUS3.5:实现了在Wabacus.report.config文件中设置报表文件配置XML和报表资源XML文件时文件路径和文件名称都可以使用通配符的功能,以方便以后将程序和报表文件按照模块化分放到指定的目录中。...

    JAVA WEB典型模块与项目实战大全

    第22章 用户登录模块(struts 2.x+guice+国际化)  22.1 用户登录概述  22.2 关于用户登录的基础知识——国际化资源  22.3 关于用户登录的基础知识——guice框架  22.4 用户登录的具体实现  22.5 小结  ...

    网络架构师148讲视频课程

    │ 第12节:Service的实现以及模块化.avi │ 第13节:Spring MVC实现Web层开发.avi │ 第14节:新增和列表页面和分页tag.avi │ 第15节:带查询的分页、修改和删除页面.avi │ 第16节:Mybatis动态查询和Json自动...

    外文翻译 stus MVC

    The struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings. (If you have not noticed it, classes that end with s are ...

Global site tag (gtag.js) - Google Analytics