`

struts.xml详解

阅读更多

一。常量配置

 

1. 在struts2中配置常量的方式有三种:

       X 在struts.xml文件中配置
       X 在web.xml文件中配置 
       X 在sturts.propreties文件中配置

     注意: 1.之所以使用struts.propreties文件配置,是因为为了保持与WebWork的向后兼容

            2.在实际开发中,在web.xml中配置常量相比其他两种,需要更多的代码量,会降低了web.xml的可读性

            3.通常推荐在struts.xml文件中配置struts2的常量,而且便于集中管理 

2. 在sturt2中搜索加载常量的顺序是:

     struts-default.xml      (在struts2-core-2.0.6.jar文件中)
     struts-plugin.xml      (在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中)
     struts.xml             (Web应用默认的Struts2的配置文件)
     sturts.propreties       (Web应用默认的Struts2的配置文件)
     web.xml                 (Web应用下的配置文件)

    注意:1.若在不同的配置文件中同时配置了相同的Struts2常量,则后一个配置文件的常量值覆盖前一个配置的常量值

3. 配置文件的属性:name和value

         name:指定属性的常量名
   value:指定属性的值

   注意:1.在不同的配置文件中配置常量,虽然方式各异,但是都必须指定name、value这两个属性

4. 各种方式示例: 

   1.struts.xml:通过constant元素配置struts2的属性
     

Xml代码  收藏代码
  1. < struts >   
  2.     < constant   name = "struts.custom.il8n.resources"   value = "mess"   />   
  3.     ....  
  4. </ struts >    
 

       

        例1:指定国际化资源文件的baseName为mess


    2.sturts.propreties:<K-V>形式,K-name V-value

        struts.devMode=ture

        例2:名为struts.devMode的常量值为ture   

    3.web.xml:通过<filter>元素的<init-param>子元素指定的
       

Xml代码  收藏代码
  1. <!-指定struts2的SilterDispatcher的Filter-! >   
  2. < filter >   
  3.   <!-指定Struts2的核心Filter-!>   
  4.     
  5.     < filter-name > strurs2 </ filter-name >   
  6.     < filter-class > org.apache.struts2.dispatcher.FilterDispaycher  
  7.     </ filter-class >   
  8.   
  9.     <!-通过init-param元素配置struts2常量-!>   
  10.   
  11.     < init-param >   
  12.         < param-name > struts.custom.il8n.resources </ param-name >   
  13.         < param-value > mess </ param-value >   
  14.     </ init-param >   
  15.   
  16. </ filter >    
 

       例3:指定国际化资源文件的baseName为mess

 

 

   常量详解

 

Xml代码  收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8"   ?>   
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">   
  5.   
  6. < struts >   
  7.     <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->   
  8.     < constant   name = "struts.i18n.encoding"   value = "UTF-8"   />   
  9.   
  10.     <!--  
  11.         该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。  
  12.         如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。  
  13.     -->   
  14.     < constant   name = "struts.action.extension"   value = "do"   />   
  15.   
  16.     <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->   
  17.     < constant   name = "struts.serve.static.browserCache"   value = "false"   />   
  18.   
  19.     <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->   
  20.     < constant   name = "struts.configuration.xml.reload"   value = "true"   />   
  21.   
  22.     <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->   
  23.     < constant   name = "struts.devMode"   value = "true"   />   
  24.   
  25.     <!-- 默认的视图主题 -->   
  26.     < constant   name = "struts.ui.theme"   value = "simple"   />   
  27.   
  28.     <!-- spring 托管 -->   
  29.     < constant   name = "struts.objectFactory"   value = "spring"   />   
  30.   
  31.     <!--  
  32.         指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration  
  33.         开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。  
  34.     -->   
  35.     < constant   name = "struts.configuration"   
  36.         value = "org.apache.struts2.config.DefaultConfiguration"   />   
  37.   
  38.     <!-- 设置默认的locale和字符编码 -->   
  39.     < constant   name = "struts.locale"   value = "zh_CN"   />   
  40.     < constant   name = "struts.i18n.encoding"   value = "GBK"   />   
  41.   
  42.     <!-- 指定Struts的工厂类 -->   
  43.     < constant   name = "struts.objectFactory"   value = "spring" > </ constant >   
  44.   
  45.     <!--  
  46.         指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name  
  47.         是默认装配模式)  
  48.     -->   
  49.     < constant   name = "struts.objectFactory.spring.autoWire"   value = "name"   />   
  50.   
  51.     <!-- 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true -->   
  52.     < cosntant   name = "struts.objectFactory.spring.useClassCache"   />   
  53.   
  54.     <!-- 指定类型检查,包含tiger和notiger -->   
  55.     < cosntant   name = "struts.objectTypeDeterminer"   value = "tiger"   />   
  56.   
  57.     <!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 -->   
  58.     < constant   name = "struts.multipart.parser"   value = "cos"   />   
  59.     < constant   name = "struts.multipart.parser"   value = "pell"   />   
  60.     < constant   name = "struts.multipart.parser"   value = "jakarta"   />   
  61.   
  62.     <!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir -->   
  63.     < constant   name = "struts.multipart.saveDir"   value = "/tmpuploadfiles"   />   
  64.   
  65.     <!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 -->   
  66.     < constant   name = "struts.multipart.maxSize"   value = "2097152"   />   
  67.   
  68.     <!--  
  69.         该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖  
  70.         struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文  
  71.         件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!)  
  72.     -->   
  73.     < constant   name = "struts.custom.properties"   
  74.         value = "application,org/apache/struts2/extension/custom"   />   
  75.   
  76.     <!--  
  77.         指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper  
  78.     -->   
  79.     < constant   name = "struts.mapper.class"   
  80.         value = "org.apache.struts2.dispatcher.mapper.DefaultActionMapper"   />   
  81.   
  82.     <!-- 指定action的后缀,默认为action -->   
  83.     < constant   name = "struts.action.extension"   value = "do"   />   
  84.   
  85.     <!-- 被 FilterDispatcher使用指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true. -->   
  86.     < constant   name = "struts.serve.static.browserCache"   value = "true"   />   
  87.   
  88.     <!-- 设置是否支持动态方法调用,true为支持,false不支持. -->   
  89.     < constant   name = "struts.enable.DynamicMethodInvocation"   value = "true"   />   
  90.   
  91.     <!-- 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true. -->   
  92.     < constant   name = "struts.enable.SlashesInActionNames"   value = "true"   />   
  93.   
  94.     <!-- 是否允许使用表达式语法,默认为true. -->   
  95.     < constant   name = "struts.tag.altSyntax"   value = "true"   />   
  96.   
  97.     <!-- 设置当struts.xml文件改动时,是否重新加载 -->   
  98.     < cosntant   name = "struts.configuration.xml.reload"   value = "true"   />   
  99.   
  100.     <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->   
  101.     < cosntant   name = "struts.devMode"   value = "true"   />   
  102.   
  103.     <!-- 设置是否每次请求,都重新加载资源文件,默认值为false. -->   
  104.     < cosntant   name = "struts.i18n.reload"   value = "false"   />   
  105.   
  106.     <!-- 标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->   
  107.     < cosntant   name = "struts.ui.theme"   value = "xhtml"   />   
  108.   
  109.     <!-- 模板目录 -->   
  110.     < cosntant   name = "struts.ui.templateDir"   value = "template"   />   
  111.   
  112.     <!-- 设置模板类型. 可以为 ftl, vm, or jsp -->   
  113.     < cosntant   name = "struts.ui.templateSuffix"   value = "ftl"   />   
  114.   
  115.     <!-- 定位velocity.properties 文件. 默认velocity.properties -->   
  116.     < cosntant   name = "struts.velocity.configfile"   value = "velocity.properties"   />   
  117.   
  118.     <!-- 设置velocity的context. -->   
  119.     < cosntant   name = "struts.velocity.contexts"   value = "...."   />   
  120.   
  121.     <!-- 定位toolbox -->   
  122.     < cosntant   name = "struts.velocity.toolboxlocation"   value = "...."   />   
  123.   
  124.     <!-- 指定web应用的端口 -->   
  125.     < cosntant   name = "struts.url.http.port"   value = "80"   />   
  126.   
  127.     <!-- 指定加密端口 -->   
  128.     < cosntant   name = "struts.url.https.port"   value = "443"   />   
  129.   
  130.     <!-- 设置生成url时,是否包含参数.值可以为: none,get or all -->   
  131.     < cosntant   name = "struts.url.includeParams"   value = "get"   />   
  132.   
  133.     <!-- 设置要加载的国际化资源文件,以逗号分隔. -->   
  134.     < cosntant   name = "struts.custom.i18n.resources"   value = "application"   />   
  135.   
  136.     <!--  
  137.         对于一些web应用服务器不能处理HttpServletRequest.getParameterMap(), 像  
  138.         WebLogic,Orion, and OC4J等,须设置成true,默认为false.  
  139.     -->   
  140.     < cosntant   name = "struts.dispatcher.parametersWorkaround"   value = "false"   />   
  141.   
  142.     <!-- 指定freemarker管理器 -->   
  143.     < cosntant   name = "struts.freemarker.manager.classname"   
  144.         value = "org.apache.struts2.views.freemarker.FreemarkerManager"   />   
  145.   
  146.     <!-- 设置是否对freemarker的模板设置缓存,效果相当于把template拷贝到 WEB_APP/templates. -->   
  147.     < cosntant   name = "struts.freemarker.templatesCache"   value = "false"   />   
  148.   
  149.     <!-- 通常不需要修改此属性. -->   
  150.     < cosntant   name = "struts.freemarker.wrapper.altMap"   value = "true"   />   
  151.   
  152.     <!-- 指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false. -->   
  153.     < cosntant   name = "struts.xslt.nocache"   value = "false"   />   
  154.   
  155.     <!-- 设置struts自动加载的文件列表. -->   
  156.     < cosntant   name = "struts.configuration.files"   
  157.         value = "struts-default.xml,struts-plugin.xml,struts.xml"   />   
  158.   
  159.     <!-- 设定是否一直在最后一个slash之前的任何位置选定namespace. -->   
  160.     < cosntant   name = "struts.mapper.alwaysSelectFullNamespace"   
  161.         value = "false"   />   
  162. </ struts >   
 

 

二。包配置

1. 图片解释:

     1.struts2框架的核心组件是 Action和拦截器等,struts2框架使用包来管理Action和拦截器等的

     2.每个包就是多个Action、多个拦截器、多个拦截器引用的集合

2. 抽象包:除了正常的包之外,struts2还提供了抽象包

         定义:不包含action定义的包

         区分:该包的package元素增加abstract="true"

3. 包配置:

      在struts.xml中,使用package元素配置包的信息,每个package元素定义一个包的配置

      package元素可以定义的属性:

         name:必填属性,指定该包的名字,是引用该包的key

         extends:可选属性,指定该包是否可以继承其他的包:可以继承一个或多个父包的中Action定义、烂机器定义、拦截器栈等配置

         namespace:可选属性,定义该包的命名空间

         abstract:可选属性,指定该包是个抽象包,包中不能有Action的定义

    注意:Struts2的配置文件是从上到下处理的,所以父包应该在子包的前面~!(其实所有xml文件都是自上往下顺序执行的)

4. 理解示例:

 

Xml代码  收藏代码
  1. < struts >   
  2.     <!-- 配置第一个包,该包名为defalut,继承struts-defalut -->   
  3.     < package   name = "default"   extends = "struts-defalut" >   
  4.   
  5.         <!-- 下面定义了拦截器部分-->   
  6.         < interceptors >   
  7.   
  8.             <!--定义拦截器栈-->   
  9.             < interceptor-stack   name = "crudStack" >   
  10.                 < interceptor-ref   name = "params"   />   
  11.                 < interceptor-ref   name = "defalutStack"   />   
  12.             </ interceptor-stack >   
  13.         </ interceptors >   
  14.   
  15.         < default-action-ref   name = "showcase"   />   
  16.   
  17.         <!--定义一个Action,该Action直接映射到showcase.jsp页面-->   
  18.         < action   name = "Showcase" >   
  19.             < result >  showcase.jsp </ result >   
  20.         </ action >   
  21.   
  22.         <!--定义一个Action,该Action类为com.opensymphony.webwork.showcase.DateAction-->   
  23.         < action   name = "Date"   class = "lee.DateAction" >   
  24.             < result   name = "success" > /date.jsp  </ result >   
  25.         </ action >   
  26.     </ package >   
  27.   
  28.     <!--配置第二个包,该包名为skill,该包继承default包-->   
  29.     < package   name = "skill"   extends = "defalut"   name = "/skill" >   
  30.   
  31.         <!-- 定义默认你的拦截器引用-->    
  32.                 < defalut-interceptor-ref   name = "crudStack" />   
  33.   
  34.         <!--定义名为Edit的Action,该类对应Action对应的处理类为lee.SkillAction  
  35. -->   
  36.         < action   name = "Edit"   class = "lee.SkillAction" >   
  37.             < result > /empmanager/editSkill.jsp </ result >   
  38.             < intercepor-ref   name = "params"   />   
  39.             < interceptor-ref   name = "basicStack"   />   
  40.   
  41.         </ action >   
  42.   
  43.         <!--定义名为Save的Action,该Action对应的处理类为lee.SkillAction,使用save方法作为处理方法-->   
  44.         < action   name = "Save"   class = "lee.SkillAction"   method = "save" >   
  45.             < result   name = "input" > /empmanager/editSkill.jsp </ result >   
  46.             < result   type = "redirect" > edit.action? skillName =${currentSkill.name}  
  47.             </ result >   
  48.         </ action >   
  49.     </ package >   
  50. </ struts >     
 

 

 

三。命名空间配置

1.不使用命名空间的方式:

struts配置:

Xml代码  收藏代码
  1. < struts >   
  2.     < include   file = "struts-default.xml"   />   
  3.     < package   name = "com.casc.manager"   extends = "struts-default"    
  4.         < action   name = "xxn"   class = "com.casc.manager.XxnAction" >   
  5.             < result   name = "success" > /success.jsp </ result >   
  6.              < result   name = "error" > /index.jsp </ result >   
  7.              < result   name  = "input"   > /index.jsp </ result   >    
  8.         </ action >   
  9.           
  10.     </ package >   
  11. </ struts >   
 
Html代码  收藏代码
  1. < form   action = "xxn.action"   method = "post" >   
  2.         < s:text   name = "user.name" > </ s:text > < input   type = "text"   name = "name" > < br >   
  3.           < s:text   name = "user.password" > </ s:text > < input   type = "password"   name = "password" > < br >   
  4.           < input   type = "submit"   value =" < s:text  name=" user .submit" /> " />   
  5. </ form >   
 

地址栏访问:http://localhost:9999/TDIAP/xxn.action

 

这样配置基本不会有问题。

可是在struts1.2里,我们习惯:path="/abc/xxn"

方便于在abc文件夹下操作。

 

2.这样在struts2.0中就要是用命名空间来达到相同的效果。

Xml代码  收藏代码
  1. < struts >   
  2.     < include   file = "struts-default.xml"   />   
  3.     < package   name = "com.casc.manager"   extends = "struts-default"   namespace = "/mng" >   
  4.         < action   name = "xxn"   class = "com.casc.manager.XxnAction" >   
  5.             < result   name = "success" > /success.jsp </ result >   
  6.              < result   name = "error" > /index.jsp </ result >   
  7.              < result   name  = "input"   > /index.jsp </ result   >    
  8.         </ action >   
  9.     </ package >   
  10. </ struts >   
 
Html代码  收藏代码
  1.   < form   action = "<%=request.getContextPath() %>/mng/xxn.action"   method = "post" >   
  2.         < s:text   name = "user.name" > </ s:text > < input   type = "text"   name = "name" > < br >   
  3.           < s:text   name = "user.password" > </ s:text > < input   type = "password"   name = "password" > < br >   
  4.           < input   type = "submit"   value =" < s:text  name=" user .submit" /> " />   
  5. </ form >   
 

这里主页 因为我们使用了命名空间“/mng”,如果在mng文件夹下的jsp页面我们可以直接写 action="xxn.action"

但在其他文件夹下就不行了。如果在mng上级目录尽量不要写成action="mng/xxn.action", 这样会成功但会出现问题,他很可能出现地址栏中http://localhost:9999/TDIAP/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng/mng /xxn.action

这种情况。

安全起见 使用绝对路径:action="<%=request.getContextPath() %>/mng/xxn.action" 这样写不用考虑当前目录。

地址栏访问:http://localhost:9999/TDIAP/mng/ xxn.action  要把命名空间加上。

 

3.注意

    1. 对于包struts2:没有指定namespace属性。如果某个包没有指定namespace属性,即该包使用默认的命名空间,默认的命名空间总是""。

2. 对于包com.casc.manager:指定了命名空间/mng,则该包下所有的Act ion处理的URL应该是“命名空间/Act ion名”。

3. struts2先在指定的路径下找act ion,如果找不到则会去默认的路径找act ion

4. 命名空间只有一个级别,如果 /a/b/get.action,系统将先在 /a/b 的命名空间下查找,如果找不到,直接进去默认命名空间查找get.action , 而不是在 /a 的命名空间下查找

5. Struts2以命名空间的方式来管理 Act ion,同一个命名空间不能有同名的Act ion。不同的命名空间 可以有 同名的 action.

 

四。包含配置

在Struts2中可以将一个配置文件分解成多个配置文件,那么我们必须在struts.xml中包含其他配置文件(注,所有的.xml文件都是标准的 Struts 2 配置文件,一样包含DTD等的声明)。

Xml代码  收藏代码
  1. < struts >   
  2.     < include   file = "struts-default.xml"   />   
  3.     < include   file = "struts-user.xml"   />   
  4.     < include   file = "struts-book.xml"   />   
  5. </ struts >   
 

 

五。拦截器配置

1:所有拦截器的超级接口Interceptor ,Action去实现这个接口;

 Interceptor 它其中有三个方法(init(),destroy() ,interceptor()):

      Init()方法:在服务器起动的时候加载一次,并且只加载一次;

      Destroy()方法:当拦截器销毁时执行的方法;

      Interceptor()方法:其中里边有一个参数invocation

Java代码  收藏代码
  1. public  String intercept(ActionInvocation invocation)  throws  xception {  
  2.   
  3.        System.out.println("interceptor!!" );  
  4.   
  5.        String result=invocation.invoke();  
  6.   
  7.        return  result;  
  8.   
  9.     }  

 

Invocation.invoke()是如果只有一个拦截器执行完这个方法后,会返回给视图,如果有多个拦截器,它顺序的执行完所有的拦截器,才返回给视图.

2: 可以在系统初始化中给拦截器指定默认的参数(也包括了定义拦截器方式)如下:

    在拦截器类中把hello当做属性set/get方式注入到拦截器类中;

Xml代码  收藏代码
  1. < interceptors >   
  2.   
  3.     <!-- 先定义拦截器 -->   
  4.   
  5.     < interceptor   name = "myInterceptor"   
  6.         class = "com.zzz.struts2.interceptor.MyInterceptor" >   
  7.   
  8.         <!-- 指定系统初始化给拦截器的参数 -->   
  9.   
  10.         < param   name = "hello" > 张钊钊 </ param >   
  11.   
  12.     </ interceptor >   
  13.   
  14.     <!-- 加到自己设置的拦截器栈里边去 -->   
  15.   
  16.     < interceptor-stack   name = "myStack" >   
  17.   
  18.         < interceptor-ref   name = "myInterceptor" >   
  19.   
  20.         </ interceptor-ref >   
  21.   
  22.         < interceptor-ref   name = "defaultStack" > </ interceptor-ref >   
  23.   
  24.     </ interceptor-stack >   
  25.   
  26. </ interceptors >   
  27.   
  28.     <!--  
  29.         改变系统默认的拦截器,改成自己的默认拦截器,并且一个系统只能有一个默认的拦截器,这样这个拦截器栈会默认应用到所有的Action上去  
  30.     -->   
  31.   
  32. < default-interceptor-ref   name = "myStack" >   
  33.   
  34. </ default-interceptor-ref >   
  35.     <!--  
  36. 也可以在使用拦截器的时候给它设置参数:  
  37.   
  38. 就是在一个action 的reslut下面配置上如下:  
  39. -->   
  40. < action   name = "register"   class = "com.zzz.struts2.action.RegisterAction" >   
  41.   
  42.     < result   name = "success" > /success.jsp </ result >   
  43.   
  44.     <!-- result 它其中还有一个信息转发类型 type=""记住,如果不转向JSP,转向图表,可以改变type=""值 -->   
  45.   
  46.     < result   name = "input" > /register.jsp </ result >   
  47.   
  48.     < interceptor-ref   name = "myInterceptor" >   
  49.   
  50.         < param   name = "hello" > welcome </ param >   
  51.   
  52.     </ interceptor-ref >   
  53.   
  54.     < interceptor-ref   name = "myStack" > </ interceptor-ref >   
  55.   
  56. </ action >   
 


     

2.拦截器,拦截器栈和默认的拦截器之间的关系

1: 拦截器和拦截器栈是一个级别的,也就是说一个拦截器栈中包括许多拦截器, 一个拦截器栈中还可以包括许多拦截器栈,配置如下方式:

Xml代码  收藏代码
  1. < interceptors >   
  2.   
  3.            <!-- 先定义拦截器 -->   
  4.   
  5.            < interceptor   name = "myInterceptor"   class = "com.zzz.struts2.interceptor.MyInterceptor" >   
  6.   
  7.               <!-- 指定系统初始化给拦截器的参数 -->   
  8.   
  9.               < param   name = "hello" > 张钊钊 </ param >   
  10.   
  11.            </ interceptor >   
  12.   
  13.            <!-- 加到自己设置的拦截器栈里边去 -->   
  14.   
  15.            < interceptor-stack   name = "myStack" >   
  16.   
  17.               < interceptor-ref   name = "myInterceptor" >   
  18.   
  19.               </ interceptor-ref >   
  20.   
  21.               < interceptor-ref   name = "defaultStack" > </ interceptor-ref >   
  22.   
  23.            </ interceptor-stack >   
  24.   
  25.        </ interceptors >   
 

拦截器的使用:1.先定义;2.在引用使用;

Xml代码  收藏代码
  1. < interceptor   name = "myInterceptor"   class = "com.zzz.struts2.interceptor.MyInterceptor" >   
  2.   
  3. < interceptor-ref   name = "myInterceptor" >   
  4.   
  5.               </ interceptor-ref >   
 

2: struts2中有一个系统默认的拦截器栈是 defaultStack,如果你手动引用自己的拦截器,系统默认的拦截器栈将不起作用;这样必需手动引入系统的拦截器栈

Xml代码  收藏代码
  1. < interceptor-ref   name = "defaultStack" >   
  2.   
  3.               </ interceptor-ref >   
 

如果想改变系统默认的拦截器栈,可以这样配置:

Xml代码  收藏代码
  1. < default-interceptor-ref   name = "myStack" >   
  2.   
  3. </ default-interceptor-ref >   

 其中myStack是自己定义的拦截器栈名字;


如果拦截器栈中有多个拦截器,在执行action之前的顺序跟配置拦截器的顺序一致,而在action之后执行的顺序是相反的;


 3:抽象的拦截器类AbstractInterceptor

1: Interceptor这个超级拦截器接口,有三方法需要实现,但是如果不想使用init();

    和destroy()方法,可以去继承这个抽象拦截器类;

它的使用跟上边的没有什么区别;

 4:方法过滤拦截器MethodFilterInterceptor

1: 上边的拦截器都要是针对整个action的,如果针对某个方法进行拦截可以去继承这个类;

它的使用跟上边的使用方法差不多,只是需要要配置它对那个方法进行拦截,方法过滤拦截器最好不要配置到自己设置默认的拦截器栈里边,自己手动配置.

Xml代码  收藏代码
  1. < interceptor-ref   name = "myInterceptor3" >   
  2.     < param   name = "includeMethods" > execute </ param >   
  3.     < param   name = "excludeMethods" > execute </ param >   
  4. </ interceptor-ref >   
  5. < interceptor-ref   name = "defaultStack" > </ interceptor-ref >   
 

其中includeMethods ,excludeMethods是固定写法: includeMethods 包含拦截那些方法,多个方法需要用”,”隔开; excludeMehtods是排除拦截的那些方法;

5:鉴听器PreResultListener接口

1: 它的鉴听点在拦截器执行完某个action方法后,在渲染视图之前做一些事情;让某个类去实现这个接口;

然后向需要它的拦截器中注册进去如下代码:

Java代码  收藏代码
  1. publicclass MyInterceptor3  extends  MethodFilterInterceptor {  
  2.   
  3.     privatestaticfinallongserialVersionUID = 3756655410194005443L;  
  4.   
  5.     @Override   
  6.   
  7.     protected  String doIntercept(ActionInvocation invocation)  throws  Exception {  
  8.   
  9.        //把鉴听器注册到拦截中去;   
  10.   
  11.        invocation.addPreResultListener(new  MyListener());  
  12.   
  13.        System.out.println("my Interceptor3" );  
  14.   
  15.        String result=arg0.invoke();  
  16.   
  17.        System.out.println("my interceptor3 finshed!" );  
  18.   
  19.        return  result;  
  20.   
  21.     }  
  22.   

  23. 转载自:http://terryjs.iteye.com/blog/772670
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics