配置文件struts.xml详解

来源:互联网 发布:81端口批量入侵摄像头 编辑:程序博客网 时间:2024/04/30 02:04

<?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>

<!--namespace可以不配置,如配:访问路经【工程名/namespace值/actionName】-->

   <package name="itcast" namespace="/test" extends="struts-default">

<!--

       通常每个包都应该继承struts-default包, 因为Struts2很多核心的功能都是拦

截器来实现 如:从请求中把请求参数封装到action、文件上传和数据验证等等都是通过拦截器实现的。 struts-default定义了这些拦截器和Result类型。可以这么说:当包继承了struts-default才能使用struts2提供的核心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。 struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。

-->

        <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >

  <result name="success">/WEB-INF/page/hello.jsp</result>

        </action>

    </package>

<!--

包还可以通过abstract=“true”定义为抽象包,抽象包中不能包含action

-->

</struts>

============================================================

Action配置中的各项默认值

1>如果没有为action指定class,默认是ActionSupport

2>如果没有为action指定method,默认执行action中的execute() 方法。

3>如果没有指定resultname属性,默认值为success

============================================================

result配置类似于struts1中的forward,struts2中提供了多种结果类型,常用的类型有:【 type属性里的值 】 dispatcher(默认值) redirect  redirectAction plainText

同一个包下的action跳转:

<result type="redirect/redirectAction">
    toThisActionName<!--目标action的name值-->
</result>

不同包下的action跳转:

type属性值只能是redirectAction,否则用redirect的话报NumPoiterException

配置文件中可以用:EL表达式来获取action中的属性值

 ========================================================================================

当多个action中都使用到了相同视图,这时我们应该把result定义为全局视图。struts1中提供了全局forward,struts2中也提供了相似功能:

<package ....>

  <global-results>

  <result name="message">/message.jsp</result>

  </global-results>

</package>

 

==============================================

前面我们都是默认使用.action后缀访问Action。其实默认后缀是可以通过常量struts.action.extension进行修改的

eg: <constant name="struts.action.extension" value="do,go"/> 逗号分开

==============建议常量【<constant name="" value=""/>】定义在struts.xml中

项目加载常量顺序:

struts-default.xml

struts-plugin.xml

struts.xml

struts.properties

web.xml

如果在多个文件中配置了同一个常量则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.

==============================================================

常用的常量介绍

<!-- 指定默认编码集,作用于HttpServletRequestsetCharacterEncoding方法和freemarker 、velocity的输出 -->

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

    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。

    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->

    <constant name="struts.action.extension" value="do"/>

    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->

    <constant name="struts.serve.static.browserCache" value="false"/>

    <!-- struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->

    <constant name="struts.configuration.xml.reload" value="true"/>

    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->

    <constant name="struts.devMode" value="true" />

     <!-- 默认的视图主题 -->

    <constant name="struts.ui.theme" value="simple" />

    <!– spring集成时,指定由spring负责action对象的创建 -->

    <constant name="struts.objectFactory" value="spring" />

 <!–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false -->

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

 <!--上传文件的大小限制-->

<constant name="struts.multipart.maxSize" value=“10701096"/>

==================================================================

为应用指定多个struts配置文件

<?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>

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

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

</struts>

 

===================================================================================
动态方法调用

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

配置文件中加上面的那常量

A:调用的时候actionName!方法名[action有扩展名的话,得在方法名后加.扩展名称]

B:使用通配符:actionName加*,再给action结点加个method属性{1}……等等

=======================================================================================

接收请求参数

采用基本类型接收请求参数(get/post)

Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。

请求路径: http://localhost:8080/test/view.action?id=78

public class ProductAction {

      private Integer id;

      public void setId(Integer id) {//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值

             this.id = id;

      }

      public Integer getId() {return id;}

  }

 

采用复合类型接收请求参数

请求路径: http://localhost:8080/test/view.action?product.id=78

 public class ProductAction {

   private Product product;

   public void setProduct(Product product) {  this.product = product;  }

   public Product getProduct() {return product;}

}

Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。

=============================================================================

自定义类型转换器

1:实现接口 com.opensymphony.xwork2.interceptor.Interceptor。

2:配置文件里配置:

EG:

原创粉丝点击