struts2配置笔记

1、struts2提供了一种模块化的方式来管理struts.xml,典型应用为:<include file="strutstest.xml" />
struts2也提供了一种插件式的方式来管理。它允许以一种"可插拔"的方式来安装插件,类似struts2-Xxx-plugin.jar的文件,只要将该jar包放入WEB-INF/lib路径下,struts将自动加载该框架
2、struts.properties文件见权威指南P93
3、struts.xml文件结构
注意配置文件写入顺序
<struts>
<constant name
<bean type
<include file
<package name
<result-type name
<interceptor name
<default-interceptor-ref name
<default-action-ref name
<global-results
<result name
<global-exception-mappings
<exception-mapping name
<action name

4、Bean配置
在struts.xml中配置bean的作用有两个:
a、创建该bean的实例,将该实例作为框架的核心组建使用
b、Bean包含的静态方法需要一个值注入
在第一种方法下,往往是作为一个核心组件使用的,需要告诉struts2容器该实例的作用,就是该实例实现了那个接口,这个接口往往定义了该组建所必须遵守的规范
例如,使用自定义的ObjectFactory来替换Struts2内置的ObjectFactory。
<struts>
<!--配置定制的ObjectFactory Bean,该Bean实现了ObjectFactory接口,实现类是MyObjectFactory-->
<bean type="com.opensymphony.xwork2.ObjectFactory" name="myfactory" class="com.company.myapp.MyObjectFactory" />
</struts>
第二种用法,可以很方便的允许不创建某个类的实例,却可以接受框架常量,此时通常要设置 static="true"
<struts>
<bean class="org.apache.struts2.dispatcher.FilterDispatcher" static="true" />
...
</struts>
参数详见P103
5、常量配置
struts2框架常量加载顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml

在struts。xml中通过constant元素配置,需要指定name,value
6、包含配置
<include file="">
7、包配置
name、extends、namespace、abstract
abstract可选,指定该包是一个抽象包,抽象包中不能包含Action定义
8、命名空间配置
namespace属性
指定为使用者自定义的命名空间,若不指定则默认为"",还可以指定根命名空间"/"
当某个包指定了命名空间后,访问该包下的Action的URL应该是:命名空间+Action
注意:
命名空间只有一级,即:/bookservice/search/get.action,首先查找/bookservice/search下的get,若找不到则到默认命名空间查找,不会在查找/bookservice

9、定义拦截器栈
先定义后使用 拦截器和拦截器栈定义和使用方式是相同的
作用:进行权限控制
跟踪日志
跟踪系统能够的性能瓶颈(记录Action处理时间)
<interceptors>
<!--权限检查-->
<interceptor name="user" class=""/>
<!--日志记录-->
<interceptor name="log" class=""/>
<!--组成拦截器栈-->
<interceptor-stack name="userandlog">
<!--定义该拦截器栈包含的拦截器-->
<interceptor-ref name="user"/>
<interceptor-ref name="log"/>
</interceptor-stack>
</interceptors>

<action name="" class="">
<result name="">...</result>
<interceptor-ref name="userandlog">
</action>

原文地址:https://www.cnblogs.com/ikuman/p/2239536.html