`
cooler1217
  • 浏览: 367845 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

struts2 配置文件 定义

 
阅读更多
在本文中将详细讲述struts.xml文件的常用配置及注意事项。
1.  使用<include>标签重用配置文件
在Struts2中提供了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签引用这些配置文件。这样做的优点如下:

结构更清晰,更容易维护配置信息。
配置文件可以复用。如果在多个Web程序中都使用类似或相同的配置文件,那么可以使用<include>标签来引用这些配置文件,这样可以减少工作量。
假设有一个配置文件,文件名为newstruts.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="demo" extends="struts-default"
>
<action name="submit"  class="action.MoreSubmitAction">
<result name="save"
>
                /result.jsp
            </result>
<result name="print">
                /result.jsp
            </result>
</action>
</package>
</struts>



则struts.xml引用newstruts.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="newstruts.xml"/>
<package name="test" extends="struts-default">
    
    </package>
</struts>


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

    在默认情况下,Struts2会调用动作类的execute方法。但有些时候,我们需要在一个动作类中处理不同的动作。也就是用户请求不同的动作时,执行动作类中的不同的方法。为了达到这个目的,可以在<action>标签中通过method方法指定要指行的动作类的方法名,并且需要为不同的动作起不同的名子(也称为别名)。如下面代码所示:

<?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="demo" extends="struts-default"
>
<action name="test"  class="action.MyAction">
       
    </action>
<action name="my"  class="action. MyAction" method="my">
        
    </action>
</package>
</struts>

上面代码的两个动作的class属性都指向同一个类,name为这个类起了两个动作别名:test和my。在动作my中,使用了method属性指定要要运行的方法名为my。

在MyAction类中必须要有my方法,代码如下:
package action;

import com.opensymphony.xwork2.ActionSupport;

public
class MyAction extends ActionSupport
{
    
    public String execute() throws Exception
    {
        // 处理test动作的代码
    }
    public String my() throws Exception
    {
          // 处理my动作的代码
    }
    
}



除了在struts.xml中配置别名,还可以通过请求参数来描述指定动作(并不需要在struts.xml中配置)。请求参数的格式如下:
http://localhost:8080/contextPath/actionName!method.action
关于通过请求指定动作的详细内容,请参阅笔者写的《Struts2教程2:处理一个form多个submit》。
3. 为action指定参数
在struts2中还可以为action指定一个或多个参数。大家还记着struts1.x是如何设置的action参数不? 在struts1.x中可以使用<action>标签的parameter属性为其指定一个action参数,如果要指定多个,就只能通过逗号(,)或其他的分隔符将不同的参数隔开。而在struts2中可以通过<param>标签指定任意多个参数。代码如下:


<action name="submit"  class="action.MyAction">
<param name="param1">value1</param>
<param name="param2">value2</param>
<result name="save"
>
        /result.jsp
    </result>
    
</action>


    当然,在action中读这些参数也非常简单,只需要象获取请求参数一样在action类中定义相应的setter方法即可(一般不用定义getter方法)。如下面的代码将读取param1和param2参数的值:
package action;

import com.opensymphony.xwork2.ActionSupport;

public
class MyAction extends ActionSupport
{
    private String param1;
    private String param2;

    public String execute() throws Exception
    {
        System.out.println(param1 + param2);
    }
    public
void setParam1(String param1)
    {
        this.param1 = param1;
    }
    public
void setParam2(String param2)
    {
        this.param2 = param2;
    }
    
}


当struts2在调用execute之前,param1和param2的值就已经是相应参数的值了,因此,在execute方法中可以直接使用param1和param2。
4. 选择result类型

在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward)。开发人员可以根据自己的需要指定不同的类型,如redirect、stream等。如下面代码所示:

<result name="save" type="redirect">
       /result.jsp
</result>
这此result-type可以在struts2-core-2.0.11.1.jar包或struts2源代码中的struts-default.xml文件中找到,在这个文件中找到<result-types>标签,所有的result-type都在里面定义了。代码如下:


<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult"
/>
<!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->
<result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult"
/>
</result-types>


5. 全局result
有很多时候一个<result>初很多<action>使用,这时可以使用<global-results>标签来定义全局的<result>,代码如下:

<struts>
<package name="demo" extends="struts-default">
<global-results>
<result name="print">/result.jsp</result>
</global-results>
<action name="submit" class="action.MoreSubmitAction">
        
        </action>
<action name="my" class="action.MoreSubmitAction" method="my">
        
        </action>
</package>
</struts>


   如果<action>中没有相应的<result>,Struts2就会使用全局的<result>。
分享到:
评论

相关推荐

    struts1配置文件定义文件

    struts1配置文件定义文件,约束了struts-config.xml文件中都包含哪些元素

    struts配置文件[定义].pdf

    struts配置文件[定义].pdf

    深入Struts2的配置文件[定义].pdf

    深入Struts2的配置文件[定义].pdf

    Struts2-rest插件(有注释)

    由于 Struts 2 的 Convention 插件的主要特点是“约定优于配置”,当我们已经习惯了 Struts 2 的基本开发方法之后,如果希望改为使用 Convention 插件也非常容易,我们只要放弃 Stuts 2.1 应用原有的配置文件,改为...

    深入Struts2配置

    Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action、多个拦截器、多个...在struts.xml文件中package元素用于定义包配置,每个package元素定义了一个包配置。

    struts2讲义_吴峻申

    1.2.4 Struts2配置文件处理 15 1.2.5 OGNL介绍和类型转换目的 15 1.2.6 进行校验 16 1.2.7 Web项目国际化根由 16 1.2.8 SiteMesh页面布局框架简介 17 1.3 我们为什么要用Struts2 17 1.4 Web项目中使用Struts2初探 20...

    struts2通用下载文件例程

    struts2通用下载文件例程 定义了Action文件和struts.xml配置文件 只需要自己写取文件流的方法就可以了!

    Struts配置详解.doc

    Struts配置文件详解 web.xml、struts-cofig.xml以及struts-cofig.xml中各个标签的作用。 Struts应用采用两个基于XML的配置文件来配置,分别是web.xml和struts-cofig.xml文件.web.xml文件是配置所有web应用的,而...

    struts2 详解文档

    解决Struts 2配置文件无提示问题 Action名称的搜索顺序 Action配置的各项默认值 result配置的各种视图转发类型 为Action属性注入值 指定Struts 2处理的请求后缀 Struts 2的处理流程与Action的管理方式 为...

    Struts2整合SiteMesh技巧

    概述 ...定义装饰器文件 缺省情况下,sitemesh假定装饰器文件保存在应用上下文根路径下的decorators目录下,如果采用如上配置,装饰器文件应该是ftl格式,如果需要使用其他格式,需要更改过滤器配置。

    深入浅出Struts2(附源码)

    2.5.1 部署描述文件和Struts配置文件 27 2.5.2 动作类 28 2.5.3 运行app02a程序 29 2.6 依赖注入 29 2.6.1 概述 29 2.6.2 依赖注入的几种方式 31 2.7 小结 31 第3章动作与结果 32 3.1 动作类 32 3.2 如何...

    Struts2的配置文件基础

    在struts.xml文件中package元素用于定义包配置,每个package元素定义了一个包配置。它的常用属性有:name:必填属性,用来指定包的名字。extends:可选属性,用来指定该包继承其他包。继承其它包,可以继承其它包中...

    struts的教程.doc

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

    Struts2的使用-实验报告.docx

    框架的配置主要包括创建 struts.xml 配置文件,其中定义了各个组件的映射关系、拦截器等。通过配置文件,我们可以指定请求的处理流程,以及请求到达时执行的操作。这为开发者提供了更大的灵活性,能够根据业务需求...

    解决struts2下载异常的jar包 struts2-sunspoter-stream-1.0.jar

    在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容...

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

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

Global site tag (gtag.js) - Google Analytics