【Struts 2.x权威指南第三版-读书笔记】3.3 Struts 2的基本配置

3.3.1 配置web.xml文件

  • 任何MVC框架都需要和Web应用整合,这就不得不借助于web.xml文件,只有配置在web.xml文件中的Servlet才会被应用加载
  • 通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于Struts 2框架而言,需要加载StrutsPrepareAndExecuteFilter,该Filter将会加载应用的Struts 2框架
  • 为了让Web应用加载StrutsPrepareAndExecuteFilter,只需要在web.xml文件中添加如下代码
  •  1 <filter>
     2     <filter-name>struts2</filter-name>
     3     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     4     <!-- 为该Filter配置初始化参数时,其中有四个初始化参数有特殊意义 -->
     5     <!--
     6     <init-param>
     7         <param-name>config</param-name>
     8         <param-value>以英文逗号隔开的字符串,每个字符串都是一个XML配置文件的位置</param-value>
     9     </init-param>
    10     <init-param>
    11         <param-name>actionPackages</param-name>
    12         <param-value>以英文逗号隔开的字符串,每个字符串都是一个包空间,Struts 2将扫描这些包空间下的Action类</param-value>
    13     </init-param>
    14     <init-param>
    15         <param-name>configProviders</param-name>
    16         <param-value>以英文逗号隔开的字符串,每个字符串都是一个类名,如果用户需要实现自己的ConfigurationProvider类,则用户可以提供一个或多个实现了ConfigurationProvider接口的类</param-value>
    17     </init-param>
    18     <init-param>
    19         <param-name>loggerFactory</param-name>
    20         <param-value>指定LoggerFactory实现类的类名</param-value>
    21     </init-param>
    22     <init-param>
    23         <param-name>常量name</param-name>
    24         <param-value>常量value,这也可配置Struts 2常量【struts.properties文件专门用于配置常量】,若想避免用struts.properties文件来定义常量,则可在此处定义常量,从而让Struts 2应用无需使用struts.properties文件</param-value>
    25     </init-param>
    26     -->
    27 </filter>
    28 <filter-mapping>
    29     <filter-name>struts2</filter-name>
    30     <url-pattern>/*</url-pattern>
    31 </filter-mapping>

3.3.2 struts.xml配置文件

    • Struts 2框架的核心配置文件就是struts.xml,该文件主要负责管理Struts 2框架的业务控制器Action
    • 在默认情况下,Struts 2框架将自动加载放在WEB-INF/classes路径下的struts.xml文件
    • 【模块化方式管理struts.xml】为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件,代码如下:
    •  1 <?xml version="1.0" encoding="UTF-8" ?>
       2 <!DOCTYPE struts PUBLIC
       3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
       4     "http://struts.apache.org/dtds/struts-2.3.dtd">
       5 
       6 <struts>
       7     <!-- 通过include元素导入其他配置文件 -->
       8     <include file="struts-part1.xml"/>
       9     。。。
      10 </struts>
  • struts-default.xml文件中定义了一个名字为struts-default的包空间,包空间内定义了Struts 2内建的Result类型系列拦截器,以及由不同拦截器组成的拦截器栈,文件的最后还定义了默认的拦截器栈引用Action默认处理类
  • struts-default.xml文件是Struts 2框架默认配置文件,Struts 2框架每次都会自动加载该文件。
  • struts.xml文件中的package元素可extends(继承)struts-default.xml文件中的struts-default包
  • Spring插件、JSF插件等,它们都提供一个类似struts-xxx-plugin.jar的文件——插件安装文件,只要将该文件放在Web应用的WEB-INF/lib路径下,Struts 2框架将自动加载该框架,struts-xxx-plugin.jar文件里面有一个struts-plugin.xml文件,它配置了Struts 2与Spring框架整合所需要的常量、拦截器等。
  • 如果用户想开发属于自己的Struts 2插件,只要将对应的struts-plugin.xml文件放在JAR文件中,Struts 2将自动加载该文件,通过这种方式,Struts 2框架允许使用可插拔的方式管理Struts 2的插件

3.3.3 struts.properties文件与常量配置

  • 配置Struts 2常量有3种方式:在struts.properties文件中配置常量在web.xml文件中配置核心控制器时通过初始化参数来配置在struts.xml中使用<constant.../>元素来配置常量
    • 在struts.xml文件中指定Struts 2的国际化资源文件的baseName为mess:(推荐使用这种方式)
      1 <struts>
      2     <constant name="struts.custom.i18n.resources" value="mess"/>
      3     ...
      4 </struts>
    • 在struts.properties文件中指定Struts 2应用处于开发阶段:
      1 struts.devMode=true
    • 在web.xml指定Struts 2常量,如配置Struts 2应用所需要的国际化资源文件的baseName:
       1 <filter>
       2     <filter-name>struts2</filter-name>
       3     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
       4     <init-param>
       5         <param-name>struts.custom.i18n.resources</param-name>
       6         <param-value>mess</param-value>
       7     </init-param>
       8 </filter>
       9 <filter-mapping>
      10     <filter-name>struts2</filter-name>
      11     <url-pattern>/*</url-pattern>
      12 </filter-mapping>
  • 通常,Struts 2框架按如下搜索顺序加载Struts 2常量:
    1. struts-default.xml:该文件保存在struts2-core-2.3.1.2.jar文件中
    2. struts-plugin.xml:该文件保存在struts2-xxx-2.3.1.2.jar等Struts 2插件JAR包中
    3. struts.xml:该文件是web应用自己的Struts 2配置文件
    4. struts.properties:该文件是web应用默认的Struts 2配置文件【哪些是有效的Struts 2常量,可参见struts2-core-2.3.1.2.jar压缩文件的org/apache/struts2路径下有一个default.properties文件,该文件为Struts 2的所有常量都指定了默认值,读者可通过查阅该文件来了解Struts 2所支持的常量】
    5. web.xml:该文件是web应用的配置文件
  • 若多个文件中都配置了同一个Struts 2常量,则最后一个文件中配置的常量值会覆盖前面文件中配置的常量值
  • default.properties的详解可参考Struts 2.x权威指南的P56

3.3.4 struts.xml文件结构

  • 可见Struts 2.x权威指南的P59

猜你喜欢

转载自www.cnblogs.com/chenhongarticles/p/9157788.html