Struts2配置文件

 Struts2配置文件

简介:

      与Struts2相关的配置文件有好几个,常用的有 struts.properties , web.xml, struts.xml等。web.xml中配置Struts2的分发器Filter。struts.properties里配置Struts2的一些属性。struts.xml里配置了Struts2的Action。Struts2的下载地址http://struts.apache.org/

目录:

  1. struts.proerties
  2. web.xml
  3. struts.xml
  4. struts常数的配置与搜索
  5. struts最小依赖包

struts.properties

      struts.properties配置了Struts2的一些参数。每个可配的参数都有一个默认值。该文件不是必须的,如果无需改变任何参数值,可以不用添加该文件。

      常用的需要在struts.properties里重新配置的属性有:

//指定默认编码集,对于请求参数带有中文的情况应该设置陈GBK或GB2312.默认值UTF-8
struts.i18n.encoding=GB2312
//是否每次HTTP请求到达时,都重新加载国际化资源文件。默认值false
struts.i18n.reload=true
//但struts.xml改动后,是否重新加载该文件。在开发阶段建议将此属性设置为“true”,提高开发效率。默认值false
struts.configuration.xml.reload=true
//是否使用Struts2的开发模式,可以获得更多报错信息,便于调试。在开发阶段设置为true。默认值false
struts.devMode = true
//设置浏览器是否缓存静态页面。开发阶段设置为false,以获得服务器的最新响应。默认值true
struts.serve.static.browserCache=true
//指定后缀为.action形式的请求可被Struts2处理。可配置多个请求后缀,比如.do、.struts2等,配置时多个后缀名用逗号隔开
struts.action.extension=action,do,struts2,
//配置服务器运行时的端口号。一般情况下该属性不修改,如果端口号占用则重新分配端口号。默认值80
struts.url.http.port = 8080

       Struts2的默认属性位于struts2-core-2.0.11.1.jar包org/apache/struts2下面的default.properties里,用户可以在项目的WEB-INF/class下添加struts.properties覆盖默认的配置。

 web.xml

      任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置在web.xml文件中的Servlet才会被加载。通常所有MVC框架都需要Web应用加载一个核心控制器,对于Struts2框架而言,需要加载StrutsPrepareAndExecuteFiter,该Filter将会加载应用的Struts2框架。

      除此之外,还可以在Web.xml中配置Struts2常量(struts.properties文件专门用于配置常量)。

    配置StrutsPrepareAndExecuteFiter的代码如下:

 <filter>
        <filter-name>struts2</filter-name>
         <filter-class>
                org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
         </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

     以上代码段存在于struts2-blankWEB-INF中的web.xml

struts.xml 

      struts.xml是Struts2的核心配置文件,里面配置Action, JSP, Exception, Intercepter等。另外,struts.properties里的配置也可以配置在struts.xml中。

一个较为完整的struts.xml配置实例如下:

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

<struts>
 
    <!-- 
    
      http://localhost:8081/hotelsys/test/demo.do
      
      1. StrutsPrepareAndExecuteFilter根据struts.xml配置文件的信息,确定凡是*.do结尾的请求,均归struts处理。
      2. 一旦发现.do结尾请求出现,直接拦截,做以下操作:
         a. http://localhost:8081/hotelsys/test/demo (去除识别标志)
         b. /test/demo  (获得真正访问资源地址信息)
         c. /test获得目录,去匹配对应的包的namespace
         d. demo 获得action的名字,去对应包中查找,如果找到,则创建该action的实例,调用其execute方法一次。
    
     -->

    <!-- 设置为开发模式,系统运行速度将降低,但是提供的出错提示更加直观,一般用于系统调试的时候使用 -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.action.extension" value="do"/>  <!--所有以*.do结尾的请求全部归struts处理  ,如果没有显式指定action识别后缀,默认就是action, 比如toInput.action-->
    <constant name="struts.ui.theme" value="simple"></constant> <!-- struts2将自动帮程序员生成网页,提供了多种生成模版 -->
    <constant name="struts.i18n.encoding" value="utf-8"></constant>
   

    <!-- 
        package是若干个拥有相关功能的action的群组
     -->
    <package name="demoPak" namespace="/test" extends="struts-default">
         <action name="demo" class="edu.fjnu.hotelsys.action.DemoAction">
         </action>
    </package>
    
    
    <package name="hotelsys-default" namespace="/" extends="struts-default">
    
        <interceptors>
       
         <interceptor name="authentication" class="edu.fjnu.hotelsys.interceptor.AuthenticationInterceptor"></interceptor>
         
         <!-- 普通需要权限的用户操作   普通的,需要身份验证才可以使用的action,将使用user拦截器stack-->
         <interceptor-stack name="user">
                <interceptor-ref name="authentication" />
                <interceptor-ref name="defaultStack" />
         </interceptor-stack>

         <!-- 普通需要权限的用户表单提交        需要身份验证才可以使用的action,同时该action中拥有表达提交操作,将使用user-submit拦截器              stack -->
         <interceptor-stack name="user-submit">
                <interceptor-ref name="user" />
         </interceptor-stack>
         
         <!-- 需要身份验证才可以使用的action,同时该action中拥有表达提交操作,同时该表单具备文件提交功能,将使用user-file-submit拦截器
stack -->
         <interceptor-stack name="user-file-submit">
              <interceptor-ref name="fileUpload">
                  <param name="allowedTypes">image/bmp,image/pjpeg,image/png</param>
                  <param name="maximumSize">200000</param>
              </interceptor-ref>
              <interceptor-ref name="user"/>
         </interceptor-stack>

         <!-- 非权限信息访问    没有任何身份需求,都可以访问的action,将使用guest拦截器stack-->
         <interceptor-stack name="guest">
                <interceptor-ref name="defaultStack" />
         </interceptor-stack>
         
       </interceptors>
       
       <!-- 没有特殊指明拦截器的action,默认使用user拦截器stack -->
       <default-interceptor-ref name="user"/>
       
       <global-results>
          <result name="gotoLoginAction" type="redirectAction">            
                <param name="actionName">gotoLogin</param>
                <param name="namespace">/security</param>
          </result>
          <result name="error" type="redirect">/error.jsp</result>
       </global-results>
       
    </package>

    <include file="security-module.xml"/>
    <include file="room-module.xml"/>
    <include file="hotel-module.xml"/> 
    <include file="user-module.xml"/> 

</struts>

      注struts.xml配置文件的默认位置在struts2-blank包下/WEB-INF/calsses/struts.xml。可以struts.xml放到MyEclipse项目的src文件夹下。并且,log4j2.xml也在此目录下。

Struts2常数的配置与搜索

配置Struts2常数有3中方式:

      1.在struts.properties文件中配置常量。

      2.在web.xml文件中配置核心Filter时通过初始化参数来配置常数。

      3.在struts.xml文件中使用<constant.../>元素来配置常量。

通常,Struts2框架按如下搜索顺序来加载Struts2常量:

      1.struts-default.xml: 该文件保存在 struts2-core-2.3.1.2.jar 文件中。

      2.struts-plugin.xml: 该文件保存在 struts2-xxx-2.3.1.2.jar文件中。

      3.struts.xml: 该文件是Web应用自己的struts2配置文件。

      4.struts.properties: 该文件是Web应用默认的Struts2配置文件。

      5.web.xml: 该文件是Web应用的配置文件。

      注:上面指定了Struts2框架搜索Struts2常量的顺序,但是如果在多个文件中配置了同一个Struts2常量,则后面一个文件中配置的常量值会覆盖前面文件中配置的常量值。

struts最小依赖包

       以struts-2.3.24.1版本为例,struts的最小依赖包的位于:struts-2.3.24.1appsstruts2-blankWEB-INFlib

   


原文地址:https://www.cnblogs.com/bmbi/p/5084083.html