struts.xml文件属性介绍

来源:互联网 发布:苹果7手机淘宝打不开 编辑:程序博客网 时间:2024/04/30 09:37

简介

struts.xml文件是使用struts2框架必需的一个xml文件,其中可以配置struts2框架的package,action,result,interceptor,以及一些常量。

基本配置

首先需要在struts.xml文件中添加dtd约束。

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd">

dtd约束可以从网上百度,也可以查文档。本人经常用的方法是直接从已导入的struts2-core-2.3.24.1.jar中查找dtd文件
struts2-core-2.3.24.1.jar

然后找到dtd约束

这里写图片描述

然后是package、action、result等的相关配置

1、package

用来管理action,result,interceptor等属性的
<package name="basicstruts2" namespace="/a" extends="struts-default">

可使用的属性有:

name:
包名
extends:
包的继承关系,类似于java的继承。通常配置包时都需要继承“struts-default.xml”包,它是struts2中的内置包
namespace:
命名空间,是url解析的一部分
http://localhost:8080/bar1.0/test/index/test 部分
abstract:
是否是抽象,用于被其他包继承
strict-method-invocation
是否拒绝任何没有被显式设置的方法,在2.5之后默认为true,需要与<allowed-methods>结合使用。但是实际使用中一般都是使用通配符的方式,所以感觉没啥用。想具体了解可以到这个网页看看

http://struts.apache.org/docs/action-configuration.html

externalReferenceResolver
与第三方jar有关,不怎么常用

可使用的标签

action
action的相关配置信息,可配置零个或多个
interceptors
interceptor过滤器的相关配置,可配置零个或一个
default-action-ref
package默认的action,一般用来设置主页,可配置零个或一个
<default-action-ref name="index"/>
default-interceptor-ref
package默认的过滤器,可配置零个或一个
<default-interceptor-ref name="myInter"></default-interceptor-ref>
default-class-ref
当package中存在没有指定class的action时,为其设置这个标签指定的class,默认为ActionSupport,可配置零个或一个
<default-class-ref class="cn.xiedacon.bar.index.action.IndexAction"></default-class-ref>
global-results
为这个package设置全局的result集合,可以用来配置提示页面,可配置零个或一个
<global-results>    <result name="msg">xxx</result></global-results>
global-exception-mappings
为这个package设置全局的exception处理器集合,可配置零个或一个
<global-exception-mappings>    <exception-mapping result="msg" exception="java.lang.NullPointerException">        <param name="msg">action抛出空指针异常</param>    </exception-mapping></global-exception-mappings>
result-types
为这个package设置result的type属性,可以用来自定义result,可配置零个或一个
<result-types>    <result-type name="myResult" class="自己定义的result类"></result-type></result-types>

2、action

用来配置action相关的信息,包括name,class,method,result等
<action name="index" class="cn.xiedacon.bar.index.action.IndexAction" method="execute">    <result>/WEB-INF/jsps/stage/index.jsp</result></action>

可以使用的属性:

name
action的名称,使用在url解析中,
http://localhost:8080/bar1.0/test/index/index 部分
class
action的class全类名
method
对应请求action时,需要执行的方法名称。可以使用通配符来达到重用action的目的
<action name="post_*" class="postAction" method="{1}">http://localhost:8080/bar1.0/test/post_findAll执行postAction的findAll方法http://localhost:8080/bar1.0/test/post_findById执行postAction的findById方法

可以使用的标签

result
表示一个处理之后的结果,可以使用零个或多个
interceptor-ref
对应于interceptor或interceptor-stack的name属性,表示执行这个action之前需要调用的拦截器
<interceptor-ref name="myInter"></interceptor-ref>
exception-mapping
指定action执行过程中抛出异常时的处理器
<exception-mapping result="msg" exception="java.lang.NullPointerException">    <param name="msg">action抛出空指针异常</param></exception-mapping>
param
表示action初始化时为action注入的属性值
<param name="initParam">初始化参数</param>

3、result

是action处理后的结果集,用来指定需要显示的页面
<action name="post_*" class="postAction" method="{1}">    <result name="findAll">/WEB-INF/jsps/stage/posts.jsp</result></action>

可以使用的属性

name
用来区分结果集的标识
type
结果集的类型,决定了strust2框架对结果的后续处理

4、interceptor

需要在interceptors标签中定义
<interceptors>    <!-- 定义拦截器        name:拦截器名称        class:拦截器全类名     -->    <interceptor name="myInter" class="xxx"/>    <!-- 定义拦截器栈,注意defaultStack -->     <interceptor-stack name="myInters">        <interceptor-ref name="myInter"/>        <!-- 使用自己定义的拦截器时,一定要引入defaultStack,这个栈是struts2的默认栈具有许多struts2的特性 -->        <interceptor-ref name="defaultStack"/>     </interceptor-stack></interceptors>

深入配置

1、include

多配置文件包含
<include file="struts-action.xml"></include>

2、constant

定义struts2框架运行时的常量
<constant name="struts.devMode" value="true" /> <!-- 部分常量struts.i18n.encoding                    指定默认编码struts.action.extension                 指定Struts 2处理的请求后缀,默认值是action,多个用,隔开struts.configuration.xml.reload         当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 struts.devMode                          开发模式下使用,默认为false,设置成true,这样可以打印出更详细的错误信息,有助于查找到错误,在开发的时候建议开启,项目发布之后改成false struts.multipart.parser                 该属性指定处理 MIME-type multipart/form-data,文件上传方式,有三种方式cos、pell 、jakarta;struts2默认采用第三种方式,前两种需下载jar包struts.multipart.saveDir                指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdirstruts.multipart.maxSize                该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 默认为2Mstruts.ui.theme                         默认的视图主题,可以为simple,xhtml或ajaxstruts.serve.static.browserCache        设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 struts.enable.SlashesInActionNames      设置是否可以在action中使用斜线,默认为false不可以,设置为truestruts.enable.DynamicMethodInvocation   设置是否支持动态方法调用,true为支持,false不支持-->
0 0
原创粉丝点击