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-name V-value

        struts.devMode=ture

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

    3.web.xml:通过元素的子元素指定的
       

Xml代码   收藏代码
  1. >   
  2. < filter >   
  3.   >   
  4.     
  5.     < filter-name > strurs2 filter-name >   
  6.     < filter-class > org.apache.struts2.dispatcher.FilterDispaycher  
  7.      filter-class >   
  8.   
  9.     >   
  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.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">   
  4.   
  5. < struts >   
  6.        
  7.     < constant   name = "struts.i18n.encoding"   value = "UTF-8"   />   
  8.   
  9.        
  10.     < constant   name = "struts.serve.static.browserCache"   value = "false"   />   
  11.   
  12.        
  13.     < constant   name = "struts.configuration.xml.reload"   value = "true"   />   
  14.   
  15.        
  16.     < constant   name = "struts.devMode"   value = "true"   />   
  17.   
  18.        
  19.     < constant   name = "struts.ui.theme"   value = "simple"   />   
  20.   
  21.        
  22.     < constant   name = "struts.objectFactory"   value = "spring"   />   
  23.   
  24.        
  25.     < constant   name = "struts.locale"   value = "zh_CN"   />   
  26.     < constant   name = "struts.i18n.encoding"   value = "GBK"   />   
  27.   
  28.        
  29.     < constant   name = "struts.objectFactory"   value = "spring" > constant >   
  30.   
  31.        
  32.     < cosntant   name = "struts.objectFactory.spring.useClassCache"   />   
  33.   
  34.        
  35.     < cosntant   name = "struts.objectTypeDeterminer"   value = "tiger"   />   
  36.   
  37.        
  38.     < constant   name = "struts.multipart.parser"   value = "cos"   />   
  39.     < constant   name = "struts.multipart.parser"   value = "pell"   />   
  40.     < constant   name = "struts.multipart.parser"   value = "jakarta"   />   
  41.   
  42.        
  43.     < constant   name = "struts.multipart.saveDir"   value = "/tmpuploadfiles"   />   
  44.   
  45.        
  46.     < constant   name = "struts.multipart.maxSize"   value = "2097152"   />   
  47.   
  48.        
  49.     < constant   name = "struts.mapper.class"   
  50.         value = "org.apache.struts2.dispatcher.mapper.DefaultActionMapper"   />   
  51.   
  52.        
  53.     < constant   name = "struts.action.extension"   value = "do"   />   
  54.   
  55.        
  56.     < constant   name = "struts.serve.static.browserCache"   value = "true"   />   
  57.   
  58.        
  59.     < constant   name = "struts.enable.DynamicMethodInvocation"   value = "true"   />   
  60.   
  61.        
  62.     < constant   name = "struts.enable.SlashesInActionNames"   value = "true"   />   
  63.   
  64.        
  65.     < constant   name = "struts.tag.altSyntax"   value = "true"   />   
  66.   
  67.        
  68.     < cosntant   name = "struts.configuration.xml.reload"   value = "true"   />   
  69.   
  70.        
  71.     < cosntant   name = "struts.devMode"   value = "true"   />   
  72.   
  73.        
  74.     < cosntant   name = "struts.i18n.reload"   value = "false"   />   
  75.   
  76.        
  77.     < cosntant   name = "struts.ui.theme"   value = "xhtml"   />   
  78.   
  79.        
  80.     < cosntant   name = "struts.ui.templateDir"   value = "template"   />   
  81.   
  82.        
  83.     < cosntant   name = "struts.ui.templateSuffix"   value = "ftl"   />   
  84.   
  85.        
  86.     < cosntant   name = "struts.velocity.configfile"   value = "velocity.properties"   />   
  87.   
  88.        
  89.     < cosntant   name = "struts.velocity.contexts"   value = "...."   />   
  90.   
  91.        
  92.     < cosntant   name = "struts.velocity.toolboxlocation"   value = "...."   />   
  93.   
  94.        
  95.     < cosntant   name = "struts.url.http.port"   value = "80"   />   
  96.   
  97.        
  98.     < cosntant   name = "struts.url.https.port"   value = "443"   />   
  99.   
  100.        
  101.     < cosntant   name = "struts.url.includeParams"   value = "get"   />   
  102.   
  103.        
  104.     < cosntant   name = "struts.custom.i18n.resources"   value = "application"   />   
  105.   
  106.        
  107.     < cosntant   name = "struts.freemarker.manager.classname"   
  108.         value = "org.apache.struts2.views.freemarker.FreemarkerManager"   />   
  109.   
  110.        
  111.     < cosntant   name = "struts.freemarker.templatesCache"   value = "false"   />   
  112.   
  113.        
  114.     < cosntant   name = "struts.freemarker.wrapper.altMap"   value = "true"   />   
  115.   
  116.        
  117.     < cosntant   name = "struts.xslt.nocache"   value = "false"   />   
  118.   
  119.        
  120.     < cosntant   name = "struts.configuration.files"   
  121.         value = "struts-default.xml,struts-plugin.xml,struts.xml"   />   
  122.   
  123.        
  124.     < cosntant   name = "struts.mapper.alwaysSelectFullNamespace"   
  125.         value = "false"   />   
  126. 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.        
  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.            
  18.         < action   name = "Showcase" >   
  19.             < result >  showcase.jsp result >   
  20.          action >   
  21.   
  22.            
  23.         < action   name = "Date"   class = "lee.DateAction" >   
  24.             < result   name = "success" > /date.jsp  result >   
  25.          action >   
  26.      package >   
  27.   
  28.        
  29.     < package   name = "skill"   extends = "defalut"   name = "/skill" >   
  30.   
  31.             
  32.                 < defalut-interceptor-ref   name = "crudStack" />   
  33.   
  34.            
  35.         < action   name = "Edit"   class = "lee.SkillAction" >   
  36.             < result > /empmanager/editSkill.jsp result >   
  37.             < intercepor-ref   name = "params"   />   
  38.             < interceptor-ref   name = "basicStack"   />   
  39.   
  40.          action >   
  41.   
  42.            
  43.         < action   name = "Save"   class = "lee.SkillAction"   method = "save" >   
  44.             < result   name = "input" > /empmanager/editSkill.jsp result >   
  45.             < result   type = "redirect" > edit.action? skillName =${currentSkill.name}  
  46.              result >   
  47.          action >   
  48.      package >   
  49. 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.   
  30. < default-interceptor-ref   name = "myStack" >   
  31.   
  32. default-interceptor-ref >   
  33.        
  34.   
  35.     < result   name = "input" > /register.jsp result >   
  36.   
  37.     < interceptor-ref   name = "myInterceptor" >   
  38.   
  39.         < param   name = "hello" > welcome param >   
  40.   
  41.      interceptor-ref >   
  42.   
  43.     < interceptor-ref   name = "myStack" > interceptor-ref >   
  44.   
  45. 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

你可能感兴趣的:(struts,web)