Struts 2 学习笔记 - 3.配置文件深入(1):struts.xml,bean,constant,package,namespace,include

1.在struts.xml文件中配置内容可以分为3类:

1)管理元素:Bean,常量,包,命名空间,包含配置;

2)用户请求处理元素:拦截器配置、Action配置、Result配置;

3)错误处理元素:异常配置;


2.Bean配置:编写自己的组件实现类来扩展或者替换框架的某一部分,使用<bean>元素来描述组件实现类。

1).实例:在struts2-core-x.x.x.jar中的struts-default.xml文件中存在大量的<bean>元素:

<struts>
...
    <bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />
    <bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" 
       class="org.apache.struts2.impl.StrutsObjectFactory" />
     <bean type="com.opensymphony.xwork2.FileManager" 
               class="com.opensymphony.xwork2.util.fs.DefaultFileManager" 
                name="system" scope="singleton"/>
...
<struts>



2).bean元素的属性:





3.常量(constant)配置:使用<constant>在struts.xml中配置,也可以在struts.properies文件中。

1)struts2框架会以以下顺序加载常量,出现相同的值时,后边的值会覆盖前面的值。前面2个是系统自带,通常在后3个中配置:

a.struts-default.xml:在struts2-core-x.x.x.jar中;

b.struts-plugin.xml在struts2-xxx-x.x.x.jar等插件jar中;

c.struts.xml:wen应用中默认的struts2配置文件;

d.struts.properties:struts2的配置属性文件;

e.web.xml:web应用的配置文件;

2)在上面的后三者文件中配置,最好在struts.xml和struts.properties中进行配置

实例1:在struts.xml文件中,指定字符编码集为utf-8:

<constant name="struts.i18n.encoding" value="utf-8" />

实例2:在struts.properties文件中实现上述常量的配置:

struts.i18n.encoding=utf-8

实例3:在web.xml文件中指定上述常量,在<filter>元素中使用<init-param>元素:

<filter>
...
	<init-param>
		<param-name>struts.i18n.encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
...
</filter>


4.包(<package>)配置:struts2中使用包(package)来管理核心组件(Action和拦截器等)。

1)一个包中可以配置多个Action、拦截器或者拦截器引用的集合等。常量不能在package中配置。

2)<package>元素的属性:

3)实例:

<package name="testPackage" extends="struts-default">
	<action name="testAction" class="testAction" method="test">
		<result name="success" type="redirect">/index.jsp</result>
	</action>
</package>


5.命名空间(namespace):如果一个Action类中有多个业务处理方法,而客户端请求需要指向不同的方法,这时就要使用namspace的方式来管理这个Action。

1)实例:

<package name="test" namespace="/tmp" extends="struts-default">
	<action name="helloworld" class="com.itzcn.action.HelloWorld">
		 <result name="success">/welcome.jsp</result>
	</action> 
</package>
进行上述代码配置后,name为helloworld的Action就可以处理下面的URL请求:

http://localhost:8080/ch3/helloworld.action

http://localhost:8080/ch3/tmp/helloworld.action
在请求helloworld.action时,先在相应的命名空间中寻找指定的Action类,如果没有找到,则到默认的命名空间中寻找。如没找到则返回错误。

2)同一个命名空间中,不能包含同名的Action;不同的命名空间中,可以包含相同的Action;

6.包含配置(<include>):在struts.xml文件这,使用<include>属性来包含其他配置文件,需要放在<struts>下,<package>外。

1)实例:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值