(联动北方技术论坛 - Powered by Landingbj) [网际游航]
struts.xml是我们在开发中利用率最高的文件,也是Struts2中最重要的配置文件。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
一下分别介绍一下几个struts.xml中常用到的标签
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
1、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
利用include标签,可以将一个struts.xml配置文件分割成多个配置文件,然后在struts.xml中使用
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
标签引入其他配置文件。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
比如一个网上购物程序,可以把用户配置、商品配置、订单配置分别放在3个配置文件user.xml、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
goods.xml和order.xml中,然后在struts.xml中将这3个配置��件引入:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
struts.xml:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
user.xml:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际��航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
2、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
在之前提到struts.properties配置文件的介绍中,我们曾经提到所有在struts.properties文件中定义的
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
属性
,都可以配置在struts.xml文件中。而在struts.xml中,是通过标签来进行配置的:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
3、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
1、包属性介绍
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
在Struts2框架中是通过包来管理action、result、interceptor、interceptor-stack等配置信息的。包属
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
性如下:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
属性
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
是否必需
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
描述
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
name 是 包名,作为其它包应用本包的标记
(联动北方技术论��� - Powered by Landingbj) [网际游航]extends 否 设置本包继承其它包
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
namespace 否 设置包的命名空间
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
abstact 否 设置为抽象包
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
2、extends属性的详解
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
•当一个包通过配置extends属性继承了另一个包的时候,该包将会继承父包中所有的配置,包括action、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
result、interceptor等。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
•由于包信息的获取是按照配置文件的先后顺序进行的,所以父包必须在子包之前被定义。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
•通常我们配置struts.xml的时候,都继承一个名为“struts-default.xml”的包,这是struts2中内置的包
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
3、namespace的详解
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
namespace主要是针对大型项目中Action的管理,更重要的是解决Action重名问题,因为不在同一个命
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
名空间的Action可以使用相同的Action名的。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
1)如果使用命名空间则URL将改变
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
比如我们有一下配置文件
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动���方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
/success.jsp
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
则此配置下的Action的URL为http://localhost:8080/login.action
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
假如为这个包指定了命名空间
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
/success.jsp
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
则此配置下的Action的URL为http://localhost:8080/user/login.action
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
2)默认命名空间
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
Struts2中如果没有为某个包指定命名空间,该包使用默认的命名空间,默认的命名空间总是。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
3)指定根命名空间
(联动北方技术论�� - Powered by Landingbj) [网际游航]当设置了命名空间为“/”,即指定了包的命名空间为根命名空间时,此时所有根路径下的Action请求都会去
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
这个包中查找对应的资源信息。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
假若前例中路径为http://localhost:8080/login.action则所有http://localhost:8080/*.action都会到
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
设置为根命名空间的包中寻找资源。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
4、与
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
1、属性介绍
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
属性
名称
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
是否必须
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
功能
描述
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
name 是 请求的Action名称
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
class 否 Action处理类对应具体路径
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
method 否 指定Action中的方法名
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
converter 否 指定Action使用的类型转换器
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
如果没有指定method则默认执行Action中的execute方法。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
2、属性介绍
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
属性
名称
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
是否必须
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
功能
描述
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
name 否 对应Action返回逻辑视图名称,默认为success
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
type 否 返回结果类型,默认为dispatcher
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
3、通配符的使用
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
随着result的增加,struts.xml文件也会随之变得越来越复杂。那么就可以使用通配符来简化配置:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
例如下面这个案例:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
Action为Test.java
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
public class Test {
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
public String test1(){
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
return result1;
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
}
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
public String test2(){
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
return result2;
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
}
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
public String test3(){
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
return result3;
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
}
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
}
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
struts.xml中配置为
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
/result{1}.jsp
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
4、访问Action方法的另一种实现方式
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
在Struts2中如果要访问Action中的指定方法,还可以通过改变URL请求来实现,将原本的“Action名
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
称.action”改为“Action名称!方法名称.action”在struts.xml中就不需要指定方法名了。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
5、与
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
这两个标签都是用来配置发生异常时对应的视图信息的,只不过一个是Action范围的,一个是包范围的,当同
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
一类型异常在两个范围都被配置时,Action范围的优先级要高于包范围的优先级.这两个标签包含的属性也是
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
一样的:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
属性
名称
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
是否必须
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
功能
描述
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
name 否 用来表示该异常配置信息
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
result 是 指定发生异常时显示的视图信息,这里要配置为逻辑视图
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
exception 是 指定异常类型
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
两个标签的示例代码为:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
6、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
当我们在配置Action的时候,如果没有为某个Action指定具体的class值时,系统将自动引用标签中所指定的类。在Struts2框架中,系统默认的class为ActionSupport,该配置我们可以
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
在xwork的核心包下的xwork-default.xml文件中找到。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
有特殊需要时,可以手动指定默认的class
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
package wwfy.action;
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
public class DefaultClassRef {
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
public void execute(){
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
System.out.println(默认class开始执行……);
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
}
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
}
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
在struts.xml中配置
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
/index.jsp
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网��游航]7、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
如果在请求一个没有定义过的Action资源时,系统就会抛出404错误。这种错误不可避免,但这样的页面并
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
不友好。我们可以使用来指定一个默认的Action,如果系统没有找到指定的Action
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
,就会指定来调用这个默认的Action。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
/jsp/actionError.jsp
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
8、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
该标签用来设置整个包范围内所有Action所要应用的默认拦截器信息。事实上我们的包继承了struts-
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
default包以后,使用的是Struts的默认设置。我们可以在struts-default.xml中找到相关配置:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
?1
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
在实际开发过程中,如果我们有特殊的需求是可以改变默认拦截器配置的。当时一旦更改这个配置,
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
“defaultStack”将不再被引用,需要手动最加。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
9、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
通过该标签可以向Struts2框架中注册拦截器或者拦截器栈,一般多用于自定义拦截器或拦截器栈的注册。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
该标签使用方法如下:
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
10、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
通过该标签可以为其所在的Action添加拦截器功能。当为某个Action单独添加拦截器功能后,中所指定的拦截器将不再对这个Action起作用。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
11、
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
该标签用于设置包范围内的全局结果集。在多个Action返回相同逻辑视图的情况下,可以通过标签统一配置这些物理视图所对应的逻辑视图。
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
/index.jsp
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
(联动北方技术论坛 - Powered by Landingbj) [网际游航]
该贴由koei转至本版2014-5-2 16:06:36