struts.xml配置文件详解
来源:互联网 发布:乳液 男士 知乎 编辑:程序博客网 时间:2024/05/16 14:52
struts.xml常用配置
- bean配置
- constant配置
- package配置
- include配置
- globaResult配置
配置概要
- Struts.xml 是Struts2的核心配置文件,默认放在classpath目录下。
- struts-default.xml—基础xml,默认配置,自动载入
- struts2-core-2.3.28.jar下
- default.properties—全局属性文件,默认配置,自动载入
- org.apache.struts2包下
- struts-plugin.xml—插件相关配置
- struts.xml—核心配置文件
- struts.properties –核心配置文件,通常定义文件在classpath下
配置
<struts><!-- 该bean实现objectfactory接口,实现类myobjectfactory,实例名factory--><bean type="com.opensymphony.xwork2.objectfactory" name="factory" class="com.opensymphony.xwork2.myapp.myobjectfactory"/></struts>
- class:必填属性,它指定Bean实例的实现类。
- type:可选属性,它指定Bean实例实现的struts2的规范,通常为struts2接口。
- name:可选属性,该属性指定Bean实例的名字
常量配置
- struts提供了一个简单的方法来改变Struts应用程序的行为,通过配置关键常量,
可以方便的修改插件及框架行为。 - 比如上传文件大小配置、启用开发者模式、启用DynamicMethodInvocation等。
<!-- 设置要加载的国际化资源文件,以逗号分隔. -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 设置是否为开发模式,默认为false,测试阶段一般设为true. -->
<constant name="struts.devMode" value="true" />
<!-- 指定action的后缀,默认为action -->
<constant name="struts.action.extension" value="do" />
<!-- 是否支持动态方法调用true为支持false不支持-->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 设置当struts.xml文件改动时,是否重新加载 -->
<cosntant name="struts.configuration.xml.reload" value="true" />
package配置
- Struts框架中核心组件就是action 和 interceptors interceptors-stack- namespace 来区别前后台相当于加了一个文件夹- name:必填属性,用来指定包的名字,唯一。- extends:可选属性, 指定包继承关系, 当包继承另一包时,该包会继承父包中的所有配置默认继承自 struts-default
Action子标签
所有的Action类都必须通过Action标签配置使用。
主要属性有:
name:必填项,用于匹配ActionMapping的请求路径
请求路径规则:package的namespace + action的name
http://localhost:8080/工程路径/namespace/请求
http://localhost:8080/工程路径/namespace/请求.action
class:非必填,配置ActionMapping关系
注:不写class的执行路径
default-action-ref标签制定的name请求
如果没有指定default-class-ref那么执行ActionSupport
method:非必填, 指定执行Action类的哪个方法,未指定则execute()
result子标签
struts2框架中,当Action处理完毕后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身。
主要属性:
Action方法执行完的返回结果配置
name属性:匹配Action方法返回结果值,指定逻辑视图名称(默认值:success)
type属性:返回结果后的处理类型(默认dispatcher)
name属性
SUCCESS:Action正确的执行完成,返回相应的视图,j是name属性的默认值。
NONE:表示Action正确的执行完成,但并不返回任何视图。
ERROR:表示Action执行失败,返回到错误处理视图。
INPUT:Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入的界面,一般在应用中,会对这些参数进行验证,如果验证没有通过,将自动返回到该视图。
LOGIN:Action因为用户没有登陆的原因没有正确执行,将返回该登陆视图,要求用户进行登陆验证。
type属性详解:
结果类型提供了返回给用户信息类型的实现细节。struts-default.xml中,可以参见细节。
默认结果类型是dispatcher,该结果类型使用JSP来向用户显示结果。
Struts2 result type属性
type属性详解:
结果类型提供了返回给用户信息类型的实现细节。struts-default.xml中,可以参见细节。
主要参数值
1. dispatcher为默认结果类型,用于加载一个视图资源(如:jsp),向用户显示页面结果。
简写形式:
<result name="success" type="dispatcher">/main.jsp</result>
加载页面过程与Action为同一个线程,Action中的相关数据可以在页面中继续使用。
使用param子元素配置结果的形式,其中param元素用于配置一个参数,配置参数需要指定参数名和参数值。参数名由name属性指定,此处的name属性可以为两种值:
location:指定该逻辑视图对应的视图名称。
parse:指定是否允许在视图中使用ognl表达式,默认为true
2. redirect类型用于重定向到另一个页面,另一个action或另一个网址。
<result name="success" type="redirect">aaa.jsp</result>
<result name="success" type="redirect">bbb.action</result>
<result name="success" type="redirect">baidu.com</result>
重定向就是全新打开另一网页,struts会产生一个新的线程,处理新的web请求,保存在原来Action执行线程中的数据,将不能再使用。
注:redirect时的数据传参:
<result name="topic" type="redirect">
/topicAction!findTopics?topicId=${topicId}
</result>
3. redirectAction:重定向到Action、不共享数据
4. chian :Action链式跳转,用于将几个action连起来执行,共同完成一个功能。
<action name="step1" class="test.S1Action">
<result name="success" type="chain">s2.action</result>
</action>
<action name="s2" class="test.S2Action">
result name="success">finish.jsp</result>
</action>
chain中的多个Action属于同一个http请求,共享相同的ActionContext、相同的数据。5. plaintext 结果类型用于直接在页面上显示源代码 <result name="error" type="plainText"> <param name="location">error.jsp</param> <param name="charSet">UTF-8</param> </result>
配置
引入其它的xml配置文件<include file="example.xml"/>
课堂笔记
- 先作jsp页面、Action类、Struts.xml配置
1.Index.jsp - 先作Action类、Struts.xml配置、jsp页面
1.product - 日常开发中,先把开发环境的字符集统一utf-8
DMI (DynamicMethodInvocation)动态方法调用:Action类中的某一个方法
a) hello!SayGoodBye
b) ActionName + “!” + 方法名
c) http://gary:8080/struts2c215demo01/product!delete- List items = new ArrayList();
<bean name="items"
type="List<String>"class="ArrayList<String>" /> - ArrayList items2 = new ArrayList();
<bean name="items" class="ArrayList<String>" />
- List items = new ArrayList();
- struts.xml配置文件详解
- 配置文件struts.xml详解
- struts.xml配置文件详解
- struts.xml配置文件详解
- Struts.xml配置文件详解
- struts.xml配置文件详解
- struts.xml配置文件详解
- Struts配置文件struts.xml详解
- struts-config.xml配置文件详解
- struts-config.xml配置文件详解
- struts-config.xml配置文件详解
- struts2 struts.xml配置文件详解
- Struts.xml中的配置文件详解
- struts-config.xml配置文件详解
- struts-config.xml配置文件详解
- struts-config.xml配置文件详解
- struts-config.xml配置文件详解
- struts-config.xml配置文件详解
- c++第5次实验
- Simditor的使用
- Ehcache 缓存
- Linux-14-创建vmware虚拟机系统续(L003-28)
- C/C++函数返回局部变量相关问题
- struts.xml配置文件详解
- 关于在adb调试过程中常用的用法及adb命令
- spring专业术语了解
- SQL Server 2012将数据导出为脚本详细图解
- 解决应用中出现英文
- 深入分析Parquet列式存储格式
- 关于ScrollView嵌套GridView和ListView不能完全显示的问题
- 0长度char数组的使用
- 视频为Activity背景