Struts2学习笔记(二)——配置详解

时间:2023-03-08 15:10:38
Struts2学习笔记(二)——配置详解

1、Struts2配置文件加载顺序:

  • default.properties(默认常量配置)
  • struts-default.xml(默认配置文件,主要配置bean和拦截器)
  • struts-plugin.xml(配置插件)
  • struts.xml(配置action或者常量等)
  • struts.properties(常量配置)
  • web.xml(配置JavaEE,如:监听器和过滤器)

2、Struts2配置文件详解

1)default.properties

default.properties是Struts2的全局常量配置文件,default.default.properties的默认配置:

 //Struts2默认的编码类型是UTF-8
struts.i18n.encoding=UTF-8
//指定jakarta为Struts的默认文件上传包,即默认使用apache的fileupload组件。
struts.multipart.parser=jakarta
//上传文件的最大字节数
struts.multipart.maxSize=2097152
//表单提交或者url请求时地址的后缀,如果需要指定多个请求后缀,则以英文逗号隔开
struts.action.extension=action,,
//Struts中action创建都是由对应的工厂创建。
struts.objectFactory = spring
//指定spring框架的自动装配类型,默认值为name,即默认根据bean的name属性自动装配
struts.objectFactory.spring.autoWire = name
//动态方法调用
struts.enable.DynamicMethodInvocation = true
//是否为开发模式
struts.devMode = false
//对于开发来讲还是必将重要的。设置为true时,在每次请求时,资源包就会被重载。
struts.i18n.reload=false
//设置为true时,我们每次修改struts.xml文件后,框架会自动加载这个文件。
struts.configuration.xml.reload=false
//页面使用静态方法。通过ognl标签调用值栈action中的方法。
struts.ognl.allowStaticMethodAccess=false

default.properties是不能直接修改的,我们如果要修改,有两种方式:

  • 在src下创建struts.properties
    • 例如:struts.enable.DynamicMethodInvocation = true
  • 在struts.xml中配置(推荐使用)
    • 例如:<constant name="struts.devMode" value=”true” />

2)struts-default.xml

struts-default.xml文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器。这些拦截器是以key-value对的形式配置在struts-default.xml中,其中name是拦截器名字,就是后面使用该拦截器的引用点,value则指定拦截器的实现类。

<?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>
<!--
package:是struts2框架底层提供出来的
* name:用于让其他包来继承的
* abstract:设置为抽象包,下面不能定义action标签
-->
<package name="struts-default" abstract="true">
<!--
result-types:声明结果类型
* name:结果类型的名称
* class:结果类型对应类的完整路径
* default:设置其为默认,true是默认
-->
<result-types>
<!-- 转发到action -->
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<!-- 转发到jsp -->
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<!-- 重定向到jsp -->
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<!-- 重定向到action -->
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<!-- 用于下载 -->
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types> <!--
interceptors
* interceptor:声明拦截器
* name:拦截器的名称
* class:对应拦截器类的完整路径
-->
<interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
<interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
<interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
<interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
<interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
<interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
<interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
<interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
<interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
<interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
<interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
<interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
<interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
<interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" /> <!-- 声明自定义拦截器 -->
<interceptor name="expessionInterceptor" class="cn.itcast.aop.ExpessionInterceptor"></interceptor> <!-- Basic stack -->
<interceptor-stack name="basicStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
</interceptor-stack> <!--
interceptor-stack:拦截器栈
* struts2框架通过使用拦截器栈,进而使用上面声明好的拦截器
* 在拦截器栈里面,存放了一些上面声明好的拦截器
* 拦截器栈相当于一个list集合,执行的时候是按照存放的先后顺序来执行
-->
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">20971520</param>
<param name="allowedTypes">text/plain</param>
<param name="allowedExtensions">.txt</param>
</interceptor-ref>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/> <!-- 配置使用自定义拦截器 -->
<interceptor-ref name="expessionInterceptor"/> </interceptor-stack> </interceptors> <!-- 配置在struts2框架运行时,默认要执行的是哪个拦截器栈,defaultStack -->
<default-interceptor-ref name="defaultStack"/> <!-- 配置在struts2框架运行时,如果没有为action指定class的话,默认要执行的class的类名 -->
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
</package> </struts>

3)struts-plugin.xml

如果不是开发插件的话,是不需要编写这个配置文件的,一般是使用插件。

4)struts.xml

struts.xml 为Struts 2的核心配置文件,主要负责管理应用中的Action映射,以及该Action包含的Result定义等。

  • 常量配置
  • 包配置
  • include包含配置
  • 拦截器配置
  • 全局result设置
 <?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>
<!--
1、constant:配置常量
* name:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"
* value:指定的是配置常量的值
* 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值 * 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开
* 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用
* 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
1 struts-default.xml
2 struts-plugin.xml
3 struts.xml
4 struts.properties(自己创建)
5 web.xml
-->
<constant name="struts.devMode" value="true"></constant> <!--
配置所有资源文件,省略后缀名,如果配置多个资源文件时,用","隔开。不仅是国际化资源文件
* 类型转换器的错误提示资源文件
* 国际化资源文件
* 上传文件的错误提示信息资源文件
-->
<constant name="struts.custom.i18n.resources"
value="cn.sunny.converter.converter,
cn.sunny.i18n.resources,
cn.sunny.upload.fileuploadmessage">
</constant> <!-- 配置文件上传的总大小 -->
<constant name="struts.multipart.maxSize" value="2097152000"></constant> <!--
2、包配置
package:包
* name:包名,唯一的,必选项
* namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分
* extends:继承其他package
* extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
-->
<package name="default" namespace="/" extends="struts-default">
<!--
action:
* name:对应页面中请求连接的后面半部分
* class:对应要执行的类的完整路径
* method:要执行的方法名称,默认为execute方法
-->
<action name="testAction" class="com.sunny.action.TestAction" method="test">
<!--
result:结果类型
* name:对应的是执行的类的方法的返回值
* 后半部分的文本内容:要转向到的页面
-->
<result name="success">/success.jsp</result>
</action>
</package> <!--
3、引入自定义配置文件
include节点是struts2中组件化的方式,
可以将每个功能模块独立到一个xml配置文件中,
然后用include节点引用
-->
<include file="struts_user.xml"/> <!--4、拦截器配置,后面讲拦截器的时候还会具体讲解-->
<interceptors>
<!-- 定义拦截器
name:拦截器名称
class:拦截器类路径
-->
<interceptor name="logger" class="com.sunny.logger"/>
<!-- 定义拦截器栈 -->
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="logger"/>
</interceptor-stack>
</interceptors>
<!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 -->
<default-interceptor-ref name="expessionStack" /> <!-- 5、全局results配置 -->
<global-results>
<result name="input">/error.jsp</result>
</global-results>
</struts>

5)struts.properties

struts.properties文件是一个标准的Properties文件,该文件包含了系列的key-value对象,每个key就是一个Struts 2属性,该key对应的value就是一个Struts 2属性值。

Struts2在default.properties文件中给出了所有属性的列表,并对其中一些属性设置了默认值,如果想改变这些默认值或者给那些没有在default.properties文件中设置值的属性设置值,则可以使用struts.properties,如果设置了struts.properties文件,那么在该文件中的属性会覆盖default.properties文件中的属性,struts.properties的使用方法和default.properties一样。

struts.properties一般放在src目录下。

6)web.xml

任何MVC框架都需要与Web应用整合需要助于web.xml文件,只有配置在web.xml文件中Servlet才会被应用加载。通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于Struts2框架而言,需要加载StrutsPrepareAndExecuteFilter,只要Web应用负责加载StrutsPrepareAndExecuteFilter,StrutsPrepareAndExecuteFilter将会加载Struts2框架。因为Struts2将核心控制器设计成Filter,而不是一个普通Servlet。故为了让Web应用加载StrutsPrepareAndExecuteFilter,只需要在web.xml文件中配置StrutsPrepareAndExecuteFilter即可。此外web.xml依然可以配置JavaEE信息,比如初始化信息,Servlet、Filter等。

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <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> </web-app>