原文链接:http://www.cnblogs.com/fmricky/archive/2010/05/20/1740479.html

1、<include>

利用include标签,可以将一个struts.xml配置文件分割成多个配置文件,然后在struts.xml中使用<include>标签引入其他配置文件。

比如一个网上购物程序,可以把用户配置、商品配置、订单配置分别放在3个配置文件user.xml、goods.xml和order.xml中,然后在struts.xml中将这3个配置文件引入:

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="user.xml"/>
<include file="goods.xml"/>
<include file="order.xml"/>
</struts>

user.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="wwfy" extends="struts-default">
<action name="login" class="wwfy.user.LoginAction">
<!--省略Action其他配置-->
</action>
<action name="logout" class="wwfy.user.LogoutAction">
<!--省略Action其他配置-->
</action>
</package>
</struts>

2、<constant>

在之前提到struts.properties配置文件的介绍中,我们曾经提到所有在struts.properties文件中定义的属性,都可以配置在struts.xml文件中。而在struts.xml中,是通过<constant>标签来进行配置的:

 <?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>
<!--设置开发模式-->
<constant name="struts.devMode" value="true"/>
<!--设置编码形式为GB2312-->
<constant name="struts.i18n.encoding" value="GB2312"/>
<!--省略其他配置信息-->
</struts>

3、<package>

1、包属性介绍

在Struts2框架中是通过包来管理action、result、interceptor、interceptor-stack等配置信息的。包属性如下:

属性

是否必需

描述

name 包名,作为其它包应用本包的标记
extends 设置本包继承其它包
namespace 设置包的命名空间
abstact 设置为抽象包

2、extends属性的详解

  • 当一个包通过配置extends属性继承了另一个包的时候,该包将会继承父包中所有的配置,包括action、result、interceptor等。
  • 由于包信息的获取是按照配置文件的先后顺序进行的,所以父包必须在子包之前被定义。
  • 通常我们配置struts.xml的时候,都继承一个名为“struts-default.xml”的包,这是struts2中内置的包。

3、namespace的详解

namespace主要是针对大型项目中Action的管理,更重要的是解决Action重名问题,因为不在同一个命名空间的Action可以使用相同的Action名的。

1)如果使用命名空间则URL将改变

比如我们有一下配置文件

 <package name="wwfy" extends="struts-default">
<action name="login" class="wwfy.action.LoginAction">
<result>/success.jsp</result>
</action>
</package>

则此配置下的Action的URL为http://localhost:8080/login.action

假如为这个包指定了命名空间

 <package name="wwfy" extends="struts-default" namespace="/user">
<action name="login" class="wwfy.action.LoginAction">
<result>/success.jsp</result>
</action>
</package>

则此配置下的Action的URL为http://localhost:8080/user/login.action

2)默认命名空间

Struts2中如果没有为某个包指定命名空间,该包使用默认的命名空间,默认的命名空间总是" "。

3)指定根命名空间

当设置了命名空间为“/”,即指定了包的命名空间为根命名空间时,此时所有根路径下的Action请求都会去这个包中查找对应的资源信息。

假若前例中路径为http://localhost:8080/login.action则所有http://localhost:8080/*.action都会到设置为根命名空间的包中寻找资源。

4、<action>与<result>

1、<action>属性介绍

属性名称

是否必须

功能描述

name 请求的Action名称
class Action处理类对应具体路径
method 指定Action中的方法名
converter 指定Action使用的类型转换器

如果没有指定method则默认执行Action中的execute方法。

2、<result>属性介绍

属性名称

是否必须

功能描述

name 对应Action返回逻辑视图名称,默认为success
type 返回结果类型,默认为dispatcher

<result>节点的主要功能是保存跳转后的路径的。一共存在两种跳转形式:客户端跳转和服务器端跳转
客户端跳转:地址栏改变,不可以传递request范围内的属性
服务器端跳转:地址栏不改变,可以传递request范围内的属性,即使跳转之后,之前的参数也可以正常的接收
默认从一个action跳转到jsp属于服务器端跳转,如果要想改变的话,可以通过type属性指定,type属性的取值一共有3种:
(1)type="redirect" 客户端跳转
<result name="success" type="redirect"> msg.jsp</result>
(2)type="dispatcher" 服务器端跳转
<result name="success" type="dispatcher"> msg.jsp</result>
(3)type="redirect-action" 将请求交给其他action继续处理
<result name="success" type="redirect-action"> msg.jsp</result>

3、通配符的使用

随着result的增加,struts.xml文件也会随之变得越来越复杂。那么就可以使用通配符来简化配置:

例如下面这个案例:

Action为Test.java

 public class Test {
public String test1(){
return "result1";
} public String test2(){
return "result2";
} public String test3(){
return "result3";
}
}

struts.xml中配置为

 <package name="wwfy" extends="struts-default">
<action name="test*" class="wwfy.action.test{1}">
<result name="result{1}">/result{1}.jsp</result>
</action>
</package>

4、访问Action方法的另一种实现方式

在Struts2中如果要访问Action中的指定方法,还可以通过改变URL请求来实现,将原本的“Action名称.action”改为“Action名称!方法名称.action”在struts.xml中就不需要指定方法名了。

5、<exception-mapping>与<global-exception-mapping>

这两个标签都是用来配置发生异常时对应的视图信息的,只不过一个是Action范围的,一个是包范围的,当同一类型异常在两个范围都被配置时,Action范围的优先级要高于包范围的优先级.这两个标签包含的属性也是一样的:

属性名称

是否必须

功能描述

name 用来表示该异常配置信息
result 指定发生异常时显示的视图信息,这里要配置为逻辑视图
exception 指定异常类型

两个标签的示例代码为:

 <?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="default" extends="struts-default">
<global-exception-mappings>
<exception-mapping result="逻辑视图" exception="异常类型"/>
</global-exception-mappings>
<action name="Action名称">
<exception-mapping result="逻辑视图" exception="异常类型"/>
</action>
</package>
</struts>

6、<default-class-ref>

当我们在配置Action的时候,如果没有为某个Action指定具体的class值时,系统将自动引用<default-class-ref>标签中所指定的类。在Struts2框架中,系统默认的class为ActionSupport,该配置我们可以在xwork的核心包下的xwork-default.xml文件中找到。

有特殊需要时,可以手动指定默认的class

 package wwfy.action;

 public class DefaultClassRef {
public void execute(){
System.out.println("默认class开始执行……");
}
}

在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>
<package name="wwfy" extends="struts-default">
<!-- 指定默认class为Test -->
<default-class-ref class="wwfy.action.DefaultClassRef"/>
<action name="test1">
<result>/index.jsp</result>
</action>
</package>
</struts>

7、<default-action-ref>

如果在请求一个没有定义过的Action资源时,系统就会抛出404错误。这种错误不可避免,但这样的页面并不友好。我们可以使用<default-action-ref>来指定一个默认的Action,如果系统没有找到指定的Action,就会指定来调用这个默认的Action。

 <?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="wwfy" extends="struts-default"> <default-action-ref name="acctionError"></default-action-ref>
<action name="acctionError">
<result>/jsp/actionError.jsp</result>
</action>
</package>
</struts>

8、<default-interceptor-ref>

该标签用来设置整个包范围内所有Action所要应用的默认拦截器信息。事实上我们的包继承了struts-default包以后,使用的是Struts的默认设置。我们可以在struts-default.xml中找到相关配置:

 <default-interceptor-ref name="defaultStack"/>

在实际开发过程中,如果我们有特殊的需求是可以改变默认拦截器配置的。当时一旦更改这个配置,“defaultStack”将不再被引用,需要手动最加。

9、<interceptors>

通过该标签可以向Struts2框架中注册拦截器或者拦截器栈,一般多用于自定义拦截器或拦截器栈的注册。该标签使用方法如下:

 <interceptors>
<interceptor name="拦截器名" class="拦截器类"/>
<interceptor-stack name="拦截器栈名">
<interceptor-ref name="拦截器名">
</interceptor-stack>
</interceptors>

10、<interceptor-ref>

通过该标签可以为其所在的Action添加拦截器功能。当为某个Action单独添加拦截器功能后,<default-interceptor-ref>中所指定的拦截器将不再对这个Action起作用。

11、<global-results>

该标签用于设置包范围内的全局结果集。在多个Action返回相同逻辑视图的情况下,可以通过<global-results>标签统一配置这些物理视图所对应的逻辑视图。

 <?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="wwfy" extends="struts-default">
<global-results>
<result name="test">/index.jsp</result>
</global-results>
</package>
</struts>

struts2系列(三):struts2配置详解的更多相关文章

  1. Hexo系列(三) 常用命令详解

    Hexo 框架可以帮助我们快速创建一个属于自己的博客网站,熟悉 Hexo 框架提供的命令有利于我们管理博客 1.hexo init hexo init 命令用于初始化本地文件夹为网站的根目录 $ he ...

  2. Struts2学习笔记二 配置详解

    Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...

  3. elasticsearch系列三:索引详解(分词器、文档管理、路由详解(集群))

    一.分词器 1. 认识分词器  1.1 Analyzer   分析器 在ES中一个Analyzer 由下面三种组件组合而成: character filter :字符过滤器,对文本进行字符过滤处理,如 ...

  4. Struts2学习笔记(二)——配置详解

    1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...

  5. Struts2中 Result类型配置详解

    一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...

  6. struts2 的struts.xml配置详解

    在应用struts框架进行开发时,必不可少的一步就是对struts.xml进行配置,对于该文件了解越多,我们开发起一应用程序定会更加顺手.下面我们看一下struts.xml的内容,每一项都有什么作用. ...

  7. Maven系列--setting.xml 配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  8. Maven系列--web.xml 配置详解

    一 .web.xml介绍 启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 紧接着,容 ...

  9. 深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)

    上篇文章<深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)>我们通过对mybatis源码的简单分析,可看出,在mybatis配置文件中,在configuration根 ...

  10. Struts2笔记——struts.xml配置详解

    访问HelloWorld应用的路径的设置 * 在struts1中,通过<action path=“/primer/helloWorldAction.action”>节点的path属性指定访 ...

随机推荐

  1. ES monitoring

    https://www.quora.com/What-is-the-best-monitoring-tool-for-Elasticsearch-I-also-want-log-monitoring- ...

  2. 缩放图片,解决bitmap 内存溢出out of memory的问题

    很多人在android开发中都遇到了生成bitmap时候内存溢出,也就是out of memory(OOM)的问题,网上对这样的问题的的解决说法不一.笔者作为一个初级开发者,在这里向大家提供一种比较实 ...

  3. scp拷贝提示its a directory 错误

    scp拷贝提示its a directory 错误 场景 使用scp的格式是 scp my_file user@ip:/home/directory 之前也一直这么用,没什么错误,莫名其妙 原因定位 ...

  4. Linux中查看GNOME版本号

    在使用图形终端时,可以在虚拟终端中直接输入gnome-about,会弹出如下窗口. 或者在纯命令行模式下使用下面命令: $ gnome-about --gnome-version 注:Gnome 3. ...

  5. Linux: grep多个关键字“与”和“或”

    1.或操作 grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep ...

  6. Jenkins自动化构建python nose测试

    [本文出自天外归云的博客园] 简介 通过Jenkins自动化构建python nose测试分两步: 1. 创建节点(节点就是执行自动化测试的机器): 2. 创建任务并绑定节点(用指定的机器来跑我们创建 ...

  7. tensorboard简单使用

    代码写的再好,没有图别人也不知道好在哪. 我们在使用tensorflow的时候,使用tensorboard可以直观的看到我们的网络结构,甚至它可以计算卷积和池化的维度(我不知道是不是因为我已经运行了一 ...

  8. 基于jQuery左侧大图右侧小图切换代码

    基于jQuery左侧大图右侧小图切换代码是一款带右侧缩略图选项卡的jQuery图片切换特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=" ...

  9. thinkphp验证码乱码的解决办法

    很有可能是入口文件index.php和.htaccess文件要转换成 以UTF-8无BOM格式编码

  10. C语言 · 字串逆序

    算法训练 字串逆序   时间限制:1.0s   内存限制:512.0MB      问题描述 给定一个字符串,将这个串的所有字母逆序后输出. 输入格式 输入包含一个字符串,长度不超过100,字符串中不 ...