xml配置文件详解
这里主要介绍一下关于struts2的xml配置信息
bean属性配置
这里主要配置了一些struts2的核心组件,把这些组件注入到struts2中
属性解释
|-class:必填属性,为bean属性的实现类
|-type:可选属性,它指定Bean实例实现的struts2的规范,该规范通常是通过某个接口来体现,因此该属性的值通常是一个struts2接口。如果需要将Bean实例作为struts2组件来使用,则应该指定该属性的值。
|-name:可选属性,给bean属性起个名称,对于有相同type类型的多个Bean,他们的name属性不能相同。
<bean type="com.opensymphony.xwork2.factory.ResultFactory" name="struts" class="org.apache.struts2.factory.StrutsResultFactory" />
<bean type="com.opensymphony.xwork2.factory.ActionFactory" name="struts" class="com.opensymphony.xwork2.factory.DefaultActionFactory" />
<bean type="com.opensymphony.xwork2.factory.ConverterFactory" name="struts" class="com.opensymphony.xwork2.factory.DefaultConverterFactory" />
<bean type="com.opensymphony.xwork2.factory.InterceptorFactory" name="struts"
class="com.opensymphony.xwork2.factory.DefaultInterceptorFactory" />
以上代码为struts2自己里面配置的一些组件,代码在struts2-core-2.3.24.jar下的struts-default.xml中可以找到。这个属性一般情况下不用自己配置。
包配置
包中包含了命名空间和继承属性,Action类就是配置在包下的
<!-- package:将Action配置封装.就是可以在Package中配置很多action.
name:必选属性,给包起个名字,起到标识作用.不能与其他包名重复.
namespace:可选属性,给action的访问路径中定义一个命名空间
extends:可选属性,继承一个指定包,基本上就是struts-default
abstract:可选属性,包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承
-->
<package name="user" namespace="/" extends="struts-default" >
action配置
action是struts2用来代替servlet的,action是一个多实例,线程安全的对象
<!-- action元素:配置action类
name属性: 决定了Action访问资源名.
class属性: action的完整类名
method属性: 指定调用Action中的哪个方法来处理请求
-->
<action name="UserAction_*" class="cn.itheima.a_hello.UserAction" method="{1}" >
name属性最后的_*是实现一个动态方法调用,比如访问UserAction_login就会执行UserAction中的login方法
result属性
result负责action类的方法执行后页面的跳转
<!-- result元素:结果配置
name属性: 标识结果处理的名称.与action方法的返回值对应.
type属性: 指定调用哪一个result类来处理结果,默认使用转发.
标签体:填写页面的相对路径
-->
<result name="success" type="dispatcher" >/hello.jsp</result>
type属性的具体值可以到struts-default中查看
include属性
<include file="cn/itheima/b_dynamic/struts.xml"></include>
用于导入其他struts的配置文件,file里面填写src下的完整路径
常量配置
默认常量配置位置
struts2的常量配置文件位置在struts2-core-2.3.24.jar/org.apache.struts2/default.properties中,可以打开此文件进行查看。
修改常量配置
constant属性
<constant name="struts.devMode" value="true"></constant>
此属性用于修改struts的常量,name为要修改常量的名字,value为想要修改的值。
struts.properties
想要修改常量也可以在src目录下创建struts.properties在里面进行键值对的配置
struts.devMode=true
web.xml修改常量
修改常量还可以在web.xml中进行配置
<context-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</context-param>
读取配置文件的加载顺序
struts框架读取配置文件的加载顺序
|-default.properties
|-struts-default.xml
|-struts-plugin.xml
|-struts.xml
|-struts.properties
|-web.xml