struts2中的struts.xml配置

本文介绍了Struts2框架中struts.xml文件的基本结构及其组成部分,包括如何使用include标签实现模块化配置,以及struts-default.xml文件中配置的主要内容。
struts.xml中包含的是开发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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<include file="example.xml"/>

<!-- Add packages here -->

</struts>


[b]include[/b]标签",它的意思是导入一个配置文件,file属性指向文件的路径。
要注意一点的是,导入的文件必需与struts.xml有两样的约束与格式,如下面是example.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="example" namespace="/example" extends="struts-default">

<action name="HelloWorld" class="example.HelloWorld">
<result>/example/HelloWorld.jsp</result>
</action>

<action name="Login_*" method="{1}" class="example.Login">
<result name="input">/example/Login.jsp</result>
<result type="redirect-action">Menu</result>
</action>

<action name="*" class="example.ExampleSupport">
<result>/example/{1}.jsp</result>
</action>

<!-- Add actions here -->
</package>
</struts>

include的好处就是可以很方面地做到模块化,以便管理,除了手动include之外,struts2中还有一些会自动include进来的文件,那就是struts-default.xml和struts-plugin.xml,struts-default.xml可以在struts2-core.jar中找到,struts-plugin.xml可以在每个插件的jar包中找到并且会在系统启动时被加载。struts-default.xml中主要配置的是:结果类、型拦截器、拦截器堆栈、包(package)以及 Web 应用执行环境(也可以在“struts.properties”中配置)的配置信息。
[b]package[/b]标签是用来把那些需要共享的通用信息——例
如拦截器堆栈或 URL命名空间——的配置组织在一起的。它通常由
Action的配置组成,但也可以包括任何类型的配置信息。
在struts-default.xml可以找到这么一段:

<package name="struts-default" abstract="true">
<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>


这个标签的属性包括有:
[list]

[*] name ——开发人员为这个 Package指定的唯一的名字。
[*] extends —— 当前这个 Package所继承的 Package的名字,
被继承的 Package中所有的配置信息(包括 Action的配
置)都可以在新的命名空间下,新的 Package里面被使
用。
[*] namespace ——命名空间提供了从 URL到 Package的映
射。也就是说,如果两个不同的 Package,其命名空间分别
为“package1”和“package2”,那么URL差不多就是
14 | 深入浅出STRUTS 2

InfoQ中文站:时刻关注企业软件开发领域的变化与创新
“/myWebApp/package1/my.action” 和
“/myWebApp/package2/my.action”这样的形式。
[*] abstract ——如果这个属性的值为“true”,那么这个
Package就只是一个配置信息的组合,也就无法通过
Package的名字来访问其中配置的 Action。可以看到struts-default.xml中package的abstract为true,因此它只是一个配置信息的组合。
[/list]
只有继承了正确的父 Package,那么你才能用到所需的预先配置
好的特性。在大多数情况下,我们都应该继承“struts-default.xml”
配置文件中的“strust-default”Package
[b]bean[/b]标签
[b]constant[/b]标签
用来覆盖default.properties里的默认的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值