[转]struts2配置文件

  http://hi.baidu.com/dragon%5F2000/blog/item/1f0a9e1676f69552f3de3245.html
代码
  1. <action name="*" class="com.ecc.struts2.web.action.ExampleSupport">  
  2.             <result>/{1}.jsp</result>  
  3.         </action>    

这种配置类似struts1.2中的ForwordAction
如果我想直接访问一个jsp文件并且又想通过*。do的方式访问。那么就可以如上这样配置
例如我想访问a.jsp,可以这样写http://localhost:8080/app/a.do,struts2找不到a这个action就会映射到a.jsp。
代码
  1. <action name="login_*" method="{1}" class="com.ecc.struts2.web.action.LoginAction">  
  2.             <result name="success">/view.jsp</result>  
  3.             <result name="login">/login.jsp</result>  
  4.         </action>  


这种配置方式类似于struts1.2中的DispatchAction
name="login_*" : 星号匹配方法名字
例如 <s:form action="login_XXX"> 会匹配到LoginAction的doXXX方法
这样你的action类可以不实现任何接口 具有更好的移植行。

Struts框架的核心配置文件就是struts.xml配置文件,该文件主要负责管理Struts 2框架的业务控制器Action。

在默认情况下,Struts 2框架将自动加载放在WEB-INF/classes路径下的struts.xml文件。在大部分应用里,随着应用规模的增加,系统中Action数量也大量增加,导致struts.xml配置文件变得非常臃肿。

为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。

下面的struts.xml文件中就通过include手动导入了一个配置文件:struts-part1.xml文件,通过这种方式,就可以将Struts 2的Action按模块配置在多个配置文件中。

<?xml version="1.0" encoding="UTF-8" ?>

<!-- 指定Struts 2配置文件的DTD信息 -->

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<!-- 下面是Struts 2配置文件的根元素 -->

<struts>

<!-- 通过include元素导入其他配置文件 -->

<include file="struts-part1.xml" />

...

</struts>  

通过这种方式,Struts 2提供了一种模块化的方式来管理struts.xml配置文件。

除此之外,Struts 2还提供了一种插件式的方式来管理配置文件。用WinRAR等解压缩软件打开struts2-core-2.0.6.jar文件,看到如图3.21所示的文件结构,在光标选中的一行,看到有一个struts-default.xml文件。

图3.21    struts2-core-2.0.6.jar压缩文件的文件结构

查看struts-default.xml文件,看到该文件代码片段如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- 指定Struts 2配置文件的DTD信息 -->

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<!-- Struts 2配置文件的根元素 -->

<struts>

<!-- 下面定义了Struts 2框架的一些基础Bean    -->

<bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />

<bean type="com.opensymphony.xwork2.ObjectFactory" name="struts"

class="org.apache.struts2.impl.StrutsObjectFactory" />

....

<!-- 下面是一些静态注入Bean定义 -->

<bean class="com.opensymphony.xwork2.util.OgnlValueStack" static="true" />

<bean class="org.apache.struts2.dispatcher.Dispatcher" static="true" />

...

<!-- 下面定义Struts 2的默认包空间 -->

<package name="struts-default">

<!-- 定义Struts 2内建支持的结果类型 -->

<result-types>

<!-- 定义Action链Result类型 -->

<result-type name="chain" class="com.opensymphony.xwork2.

ActionChainResult"/>

<!-- 定义Dispatcher的Result类型,并设置default="true",

指定该结果Result是默认的Result类型 -->

<result-type name="dispatcher"

class="org.apache.struts2.dispatcher.Servlet

DispatcherResult" default="true"/>

<!-- 定义FreeMarker的Result类型 -->

<result-type name="freemarker"

class="org.apache.struts2.views.freemarker.

FreemarkerResult"/>

...

</result-types>

<!-- 定义Struts 2内建的拦截器 -->

<interceptors>

<interceptor name="alias" class="com.opensymphony.xwork2.

interceptor.AliasInterceptor"/>

<interceptor name="autowiring"

class="com.opensymphony.xwork2.spring.interceptor.

ActionAutowiringInterceptor"/>

...

<!-- 定义基本拦截器栈 -->

<interceptor-stack name="basicStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="servlet-config"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="params"/>

<interceptor-ref name="conversionError"/>

</interceptor-stack>

<!-- 还有系列拦截器栈 -->

...

</interceptors>

<!-- 定义默认的拦截器栈引用 -->

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

</package>

</struts>   

上面的代码并未全部列出struts-default.xml文件,只是列出了每个元素的代表。上面配置文件中定义了一个名字为struts-default的包空间,该包空间里定义了Struts 2内建的Result类型,还定义了Struts 2内建的系列拦截器,以及由不同拦截器组成的拦截器栈,文件的最后还定义了默认的拦截器引用。

这个struts-default.xml文件是Struts 2框架的默认配置文件,Struts 2框架每次都会自动加载该文件。查看我们前面使用的struts.xml文件,看到我们自己定义的package元素有如下代码片段:

<!-- 指定Struts 2配置文件的根元素 -->

<struts>

<!-- 配置名为lee的包空间,继承struts-default包空间 -->

<package name="lee" extends="struts-default">

...

</package>

</struts>

在上面配置文件中,名为lee的包空间,继承了名为struts-default的包空间,struts-default包空间定义在struts-default.xml文件中。可见,Struts 2框架默认会加载struts-default.xml文件。

不仅如此,Struts 2框架提供了一种类似Eclipse的扩展方式,它允许以一种“可插拔”的方式来安装插件,例如后面将要介绍的Spring插件、JSF插件等,它们都提供了一个类似struts2-Xxx-plugin.jar的文件——这个文件就是插件安装文件,只要将该文件放在Web应用的WEB-INF/lib路径下,Struts 2框架将自动加载该框架。

使用WinRAR工具打开struts2-spring-plugin2.06.jar文件,找到一个struts-plugin.xml文件,打开该文件,该文件的代码如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- 指定Struts 2的DTD信息 -->

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!-- 定义一个名字为spring的ObjectFactory -->

<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring"

class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

<!-- 指定名字为sping的ObjectFactory是Struts 2的ObjectFactory -->

<constant name="struts.objectFactory" value="spring" />

<!-- 定义名为spring-default的包空间 -->

<package name="spring-default">

<!-- 定义整合Spring框架所必需的拦截器列表 -->

<interceptors>

<interceptor name="autowiring"

class="com.opensymphony.xwork2.spring.interceptor.

ActionAutowiringInterceptor"/>

<interceptor name="sessionAutowiring"

class="org.apache.struts2.spring.interceptor.

SessionContextAutowiringInterceptor"/>

</interceptors>

</package>

</struts>  

在上面配置文件中,配置了Struts 2与Spring框架整合必需的常量、拦截器等——这也是Struts 2框架使用Spring插件必需的配置。只要将struts2-spring-plugin2.06.jar文件放在WEB-INF/lib路径下,Struts 2框架将自动加载该文件。

提示    如果用户开发属于自己的Struts 2插件,只要将对应的struts-plugin. xml文件放在JAR文件中,Struts 2框架将自动加载该文件。通过这种方式,Struts 2框架允许使用可插拔的方式管理Struts 2的插件。

一、Struts2配置文件
Struts2相关的配置文件有web.xml,struts.xml,struts.properties,
struts-default.xml,velocity.properties,struts-default.vm。其中web.xml,struts.xml是必须的,其它的配置文件可选择。它们在web应用中的功能如下:
web.xml:包含所有必须的框架组件的web部署描述符。
Struts.xml:配置包含result/view类型、action映射、拦截器等的Struts2的主要配置文件。
Struts.properties:配置struts2的框架属性。
Struts-default.xml:在文件在struts-action-x.x.jar中,该文件是应该被包含在struts.xml中的缺省配置。
Welocity.properties:重写了velocity的配置文件。
Struts-default.vm:相对于velocity的缺省配置。

二、Struts2配置元素
Struts2核心的配置文件是缺省的struts.xml。
必 要的时候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,并自动插入应用程序,这样每个模块可以包含自己的配置文件并自动配 置。在Freemarker和Velocity模块中,模板也能从classpath中加载,所以整个模块可以作为一个简单的jar文件被包含。
Struts.xml配置文件可以包含Interceptor、Action类和Results。
Struts.xml配置元素说明:

1、Packages
Packages:packages把actions、results、results types、interceptors
和interceptor-stacks组装到一个逻辑单元中,从概念上讲,packages就像一个对象,可以被其它子包从写,而且可以拥有自己独立的部分。
Name属性是packages的必填元素,它作为一个关键字被后边的包引用;extends元素是可选的,它允许包扩展一个和多个前边定义的包。注意,
struts.xml文件是至上而下处理的,所有被扩展的包,需要在扩展包前定义。
Abstract元素是可选的,它可以申明一个不包含actions的配置文件。

2、Namespace
Namespace元素把actions细分到逻辑模块,每一个namespace都有自己的
前缀(prefix),namespace避免了action之间的名字冲突,当前缀出现在URI中时,这些标签都是名字空间感知的(“namespace aware”),所以这些namespace prefix不必嵌入到表单或连接中。
Default 的namespace是一个空字符串“”,如果在指定的配置文件中,没有找到action,缺省的namespace也会被查找。 Local/global策略允许应用程序在action “extends”元素层次结构之外,有全局的action配置。缺省的namespace也可以不在package中申明。
Namespace prefix可以注册为java的申明式安全,以确保授权的用户才能访问namespace的资源。
Root namespace(“/”)也被支持,root就是当直接请求context path的时候的namespace。

[个人理解:namespace的用法就像struts1.x中的path一样,只不过它在package中什么路径,而在action中申明action名子罢了]

3、Include
Include元素使得框架能应用“divide and conquer”来配置文件。被include
的每个配置文件必须和struts.xml有一样的格式,一个大的项目可以采用这样方式来组织程序模块。
Include元素也可以和package交替出现,框架将按照顺序加载配置文件。

4、Interceptor configuration
Interceptor允许应用程序在Action方法执行前后定义执行代码,
Interceptor在应用程序开发中十分重要,对于Interceptor有许多用例,如validation, property population, security, logging, 和profiling。
Interceptor被定义为一个Java类,Interceptor也可以组装成Interceptor-stack,他们将按照定义的顺序执行。
在struts-default.xml中定义了一些缺省的Interceptor-stack,以便框架能很好地运行。

5、Action
Action是框架的“工作单元”。Action可以指定一个Interceptor-stack、
一序列的return type和一序列的异常处理,但只有name属性是必须的。  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值