Struts2 配置相关问题

来源:互联网 发布:淘宝个人主页怎么隐藏 编辑:程序博客网 时间:2024/05/01 00:37

Struts2 配置相关问题

原文地址:http://terryjs.iteye.com/blog/729839

 

一、 在 web.xml 配置 struts.xml

 

struts-default.xml,struts-plugin.xml 系统默认必需加上,后面写自己的struts.xml

位置 是从 WEB-INF 的 classes 开始计算


struts.xml 的路径为 WEB-INF/struts-configs/struts.xml (沿用struts1的习惯)

struts-default.xml文件位置 不用动 , 不需要 放在WEB-INF/struts-configs/中


更改web.xml

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        <init-param>
               <param-name>config</param-name>
               <param-value>struts-default.xml,struts-plugin.xml,../struts-configs/struts.xml</param-value>
         </init-param>
 </filter>

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

 

 

普通 Struts 2 配置 web.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!-- 配置Web应用配置文件的根元素,并指定配置文件的Schema信息 -->  
  3. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">  
  7.     <!-- 定义Struts 2的核心控制器:FilterDispatcher -->  
  8.     <filter>  
  9.         <!-- 定义核心Filter的名字 -->  
  10.         <filter-name>struts2</filter-name>  
  11.         <!-- 定义核心Filter的实现类 -->  
  12.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  13.     </filter>  
  14.     <!-- FilterDispatcher用来初始化Struts 2并且处理所有的HTTP请求 -->  
  15.     <filter-mapping>  
  16.         <filter-name>struts2</filter-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </filter-mapping>  
  19. </web-app>  

 

 

2.  Struts 2.2.1 配置 web.xml 之类

与 2.1 略有不同

web.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18. </web-app>  

 struts.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.     <package name="js" extends="struts-default" namespace="/09">  
  5.         <action name="ifAction" class="js.IfAction">  
  6.             <result name="success">/09/s-if.jsp</result>  
  7.         </action>  
  8.     </package>  
  9. </struts>      

 

Html代码  收藏代码
  1. <s:a action="09/ifAction">s-if.jsp</s:a>     

 

 

 

 

二、 struts2.2.1 报错

 

 

1. 缺少 javassist-3.7.ga.jar 包

如有以下报错:

Txt代码  收藏代码
  1. 2010/12/15 11:25:37 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error  
  2. 致命的: Dispatcher initialization failed  
  3. java.lang.RuntimeException: java.lang.reflect.InvocationTargetException  
  4.     at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:295)  
  5.     at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:431)  
  6.     at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:207)  
  7.     at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)  

 

Struts2最新版本2.2.1中所设计的核心库文件,包括以下内容:
struts2-core-2.2.1.jar:Struts2框架的核心类库。
xwork-core-2.2.1.jar:Xwork核心类库,Struts2在其上构建。
ognl-3.0.jar:对象图导航语言(Object Graph Navigation Language),它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。
freemarker-2.3.16.jar:Struts 2的UI标签的模板使用FreeMarker编写。
commons-fileupload-1.2.1.jar:文件上传组件,2.1.6版本后必须加入此文件。
commons-io-1.3.2.jar:IO输入输出流组件,主要完成文件的读写功能。
javassist-3.7.ga.jar:Javassist是一个开源的分析、编辑和创建Java字节码的类库。
-------------------------------------------------------------------
commons-logging-1.0.4.jar:ASF出品的日志包,Struts2框架使用这个日志包来支持Log4J和JDK 1.4版本之上的日志记录

注意: javassist-3.7.ga.jar struts2源码包里没有,可以去下载。。也可以ognl-3.0.jar  替换成ognl-2.6.11.jar,这样就不必要加javassist-3.7.ga.jar

 

 少了javassist-3.7.ga.jar,可以在struts2-2.2.1-all.zip包中找到

 

  方法: 打开 struts2-2.2.1-all\struts-2.2.1\apps\目录下随便一个.war 文件 用解压软件,然后在 WEB-INF/lib下找到 javassist-3.7.ga.jar

 

 

2. 没有使用 spring 去掉一些包

Txt代码  收藏代码
  1. 2010/12/15 11:44:13 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error  
  2. 致命的: Dispatcher initialization failed  
  3. java.lang.NullPointerException  
  4.     at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:220)  
  5.     at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:530)  
  6.     at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:501)  

 

原因两个:
1. lib 中多导入包的大原因:去掉 struts2-spring-plugin-2.1.8包即可,因为没有用到spring
2. 还有的原因是用spring了,却没加监听器,在 web.xml 里面加上

Xml代码  收藏代码
  1. <listener>  
  2.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>   

 

 

3. struts 2.2.1 基本功能使用的包

  commons-fileupload-1.2.1.jar

  commons-io-1.3.2.jar

  freemarker-2.3.16.jar

  javassist-3.7.ga.jar

  ognl-3.0.jar

  struts2-core-2.2.1.jar

  xwork-core-2.2.1.jar

 

 

三、 struts 2 中 jsp 提交 list ,map 到 Action

 

详情请看Struts 2 的标签库(二)中的 3.2  iterator 标签

 

Html代码  收藏代码
  1. <s:form action="iteratorAction.action" method="post">  
  2.   
  3.     <h3>action の List</h3>  
  4.     <s:iterator value="list" id="aList" status="st">  
  5.         index:&nbsp;&nbsp;&nbsp;&nbsp;  
  6.         <s:property value="#st.index"/>   
  7.         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  8.         value:&nbsp;&nbsp;&nbsp;&nbsp;  
  9.         <s:property value="aList"/>  
  10.         <br/>  
  11.           
  12.         <!-- 下面是把  list 传回 Action -->  
  13.         <s:hidden name="list[%{#st.index}]" value="%{list[#st.index]}"></s:hidden>        
  14.     </s:iterator>  
  15.     <br/><br/>    
  16.     <h3>action の Map</h3>  
  17.     <s:iterator value="map" id="aMap" status="st">  
  18.         index:&nbsp;&nbsp;&nbsp;&nbsp;  
  19.         <s:property value="#st.index"/>   
  20.         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  21.         key:&nbsp;&nbsp;&nbsp;&nbsp;  
  22.         <s:property value="key"/>  
  23.         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  24.         value:&nbsp;&nbsp;&nbsp;&nbsp;  
  25.         <s:property value="value"/>  
  26.         <br/>  
  27.           
  28.         <!-- 下面是把  map 传回 Action -->  
  29.         <input type="hidden" name="map['<s:property value="key"/>']" value="<s:property value="value"/>"></input>  
  30.           
  31.     </s:iterator>  
  32.     <br/><br/>  
  33.     <input type="submit" value="submit"></input>  
  34. </s:form>  

下面这段是关键:

list

1.

 <s:iterator value="list" id="aList" status="st">      
        <!-- 下面是把  list 传回 Action -->
        <s:hidden name="list[%{#st.index}]" value="%{list[#st.index]}"></s:hidden>        
    </s:iterator>
    <br/><br/>    
 

2.

<input type="hidden" name="list2[<s:property value="#st.index" />]" value="<s:property value="tl2"/>"></input>

  

map
    <s:iterator value="map" id="aMap" status="st">       
        <!-- 下面是把  map 传回 Action -->
        <input type="hidden" name="map['<s:property value="key"/>']" value="<s:property value="value"/>"></input>       
    </s:iterator>

 

 

Java代码  收藏代码
  1. package js;  
  2.   
  3. import java.util.*;  
  4.   
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7. public class IteratorAction extends ActionSupport {  
  8.   
  9.     private List<String> list ;  
  10.     private Map<String,String> map ;  
  11.       
  12.     @Override  
  13.     public String execute() throws Exception {  
  14.         if (list == null || list.isEmpty()) {  
  15.             System.out.println("list is null !");  
  16.             list = new ArrayList<String>();  
  17.             list.add("あ");  
  18.             list.add("い");  
  19.             list.add("う");  
  20.             list.add("え");  
  21.             list.add("お");  
  22.         } else {              
  23.             System.out.println("list is not null !");  
  24.             System.out.println(list.toString());  
  25.         }  
  26.        
  27.         if (map == null || map.isEmpty()) {  
  28.             System.out.println("map is null !");  
  29.             map = new HashMap<String, String>();  
  30.             map.put("a""か");  
  31.             map.put("b""き");  
  32.             map.put("c""く");  
  33.             map.put("d""け");  
  34.             map.put("e""こ");  
  35.         } else {  
  36.             System.out.println("map is not null !");  
  37.             System.out.println(map.toString());  
  38.         }  
  39.           
  40.         return SUCCESS;  
  41.     }  
  42.       
  43.       
  44.     public List<String> getList() {  
  45.         return list;  
  46.     }  
  47.   
  48.     public void setList(List<String> list) {  
  49.         this.list = list;  
  50.     }  
  51.   
  52.     public Map<String, String> getMap() {  
  53.         return map;  
  54.     }  
  55.   
  56.     public void setMap(Map<String, String> map) {  
  57.         this.map = map;  
  58.     }  
  59. }  

 

Xml代码  收藏代码
  1. <package name="js"  extends="struts-default" namespace="/09">  
  2.     <action name="iteratorAction" class="js.IteratorAction">  
  3.         <result name="success">/09/s-iterator.jsp</result>    
  4.     </action>  
  5. </package>  

 

 

四、 ActionContext、ValueStack、Stack Context

 

看源码你就会发现,ActionContext这个类中有一个变量:
Map context,
实际上调用 ActionContext的put,get 就相当于调用context的相就方法,你在页面上通过获取的就相当于调用 context.get("request"),context.get("attr"),而你通过ac.put("list", List)也只能通过ac.get("list")获得。
而ActionContext.getValueStack()相当于调用context.get(OgnlValueStack.VALUE_STACK)所获取的与通过ActionContext.get("request")获取的是不一样的东西
=======================================================================================

ActionContext
一次Action调用都会创建一个ActionContext
调用:ActionContext context = ActionContext.getContext()

ValueStack
由OGNL框架实现
可以把它简单的看作一个List


Stack Object:放入stack中的对象,一般是action。
Stack Context(map):stack上下文,它包含一些列对象,包括request/session/attr/application map等。
EL:存取对象的任意属性,调用对象的方法,遍历整个对象结构图
=======================================================================================
ActionContext是Action上下文
可以得到request session application
ValueStack是值栈 存放表单中的值
Stack Context 栈上下文 也是用来存值的
=======================================================================================
你可以通过在页面中添加<s:debug/>标签察看ValueStack 和Stack Context中有哪些具体的内容。ValueStack里面的东西先入后出,很多没有id的元素使用完之后就退栈了。
StackContext提供了一些命名对象,这些命名对象与根对象无关,访问这些对象需要使用#前缀指名。比如这里面存在:parameters对象,request对象, session对象,application对象等等。
=======================================================================================
value stack contents   ognl 值栈
stack context           action上下文

action上下文是一个map对象,通过#key获得对象内容,在#request又可以得到值栈,值栈里存储的是一些action里的变量
于是获得action变量内容有三种方式
1,从值栈中直接获得
  <s:property value="type"/>
  ${type }
2,从action上下文获得值栈的属性
<s:property value="#request['type']"/>或者<s:property value="#request.type"/>
=======================================================================================
 1)  ValueStack 可以用 request.getAttribute("struts.valueStack") 取得的一个 com.opensymphony.xwork2.util.OgnlValueStack 实例,它实现为一个栈,有 peek()、pop()、push(obj) 方法。

   2) Stack Context 是在前面的 ValueStack 的上下中的一个 java.util.Stack 实例:
  //stack 为 ValueStack 实例,COMPONENT_STACK="__component_stack"
  // org.apache.struts2.components.Component.getComponentStack() 中的代码
  Stack componentStack = (Stack) stack.getContext().get(COMPONENT_STACK);

 

五、 struts2 的 ONGL 标签取值

 


经常搞不清楚 %, # 怎么用。

1. 先解决最基础的温饱问题:如何把 action中的值带到页面展示出来

(先定义一个action,有个username 字段,假设value="song"想传递到页面上)

Java代码  收藏代码
  1. public class Login extends ActionSupport {  
  2.   
  3.     private String username;  
  4.       
  5.     public String execute() throws Exception {  
  6.         return INPUT;  
  7.      }  
  8.   
  9.     public String getUsername() {  
  10.         return username;  
  11.      }  
  12.   
  13.     public void setUsername(String username) {  
  14.         this.username = username;  
  15.      }  
  16.   
  17. }  

 下面是页面上显示username的一些尝试:
=========直接把值显示出来。。。========
1.s:property 标签

Xml代码  收藏代码
  1. <s:property value="username"/>      

 

2.$ 符号表达式   

Ognl代码  收藏代码
  1. ${username}  

 

    上面的两种方式都是可以显示"song"出来的,如果只用来显示,推荐${username} ,简单明了。不过${}不可以放在struts2 自带的<s:xxx >标签中混用,会报不支持变量表达式异常

========== # 试一下。。。==============
1.# 表达式      

Ognl代码  收藏代码
  1. #username     

失败,页面输出 "#username"


2.s:property标签1: 

Html代码  收藏代码
  1. <s:property value="#username"/>   

失败,页面无输出


3.s:property标签2:   

Html代码  收藏代码
  1. <s:property value="#request.username"/>  

 成功,页面输出"song"(这种属性放进了request中)


4.s:property标签3: 

Html代码  收藏代码
  1. <s:property value="#session.username"/>  

 失败,页面无输出(没有放到session中)

                 
========= %试一下。。。==========
                           
1.%表达式       

Ognl代码  收藏代码
  1. %{username}   

失败,页面输出"%{username}"


2.% 表达式   

Ognl代码  收藏代码
  1. %{'username'}    

失败,页面输出 "%{'username'}"


3.s:property标签4:       

Html代码  收藏代码
  1. <s:property value="%{username}"/>  

成功,页面输出"song"


4.s:property标签 Error4: 

Html代码  收藏代码
  1. <s:property value="%{'username'}"/>  

失败,页面输出"username"


测试了一大把,最后总结出了4种拿到值得办法:
<s:property value="username"/>
${username}  
<s:property value="#request.username"/> (#session. #application.类似)
<s:property value="%{username}"/>

 

 

六、struts.xml 中的 include 标签和package 标签以及 action 标签的 name 属性

 

 

struts.xml中的<include/>标签
当系统变大后,可能会导致struts.xml内容非常多,这时我们就可以采用模块化的方式
将不同的配置文件分散在不同的配置里面,然后由struts.xml将它们统一的加载进来
比如struts_1.xmlstruts_2.xmlstruts_3.xml三个文件,分别是针对不同模块的配置
最后就可以由struts.xml统一将其整合起来,整合的方式就是把这它们包含进来即可
<include file="struts_1.xml"/><include file="struts_2.xml"/><include file="struts_3.xml"/>

struts_1.xml或者struts_2.xml中的配置方式与struts.xml中的配置方式是一模一样
即它们顶部都必须有XML声明DTD,它们DTD都是相同的,接下来是<struts/>根元素

即不能认为struts_1.xml是被包含文件,便将顶部XML声明和DTD省略掉,这是不可以的
最后由struts.xml统一引入,于是系统加载,便将struts_1.xml及struts_2.xml等的配置都加载到内存中
这就相当于进行模块化的配置。对于大中型的系统来说,是不可避免的会使用模块化的配置方式的


struts.xml中<package name="#"/>的命名
其中struts.xml中<package/>的name可随意命名,不会影响到其它的任何东西
它的name属性的作用仅仅是用来标识<package/>的,以便另外的<package/>可以继承


<package name="struts-default" abstract="true">
在struts-default.xml的第73行配置<package/>时,声明了一个abstract="true"属性
我们发现这个包中声明了结果类型、拦截器等等,却唯独没有声明<action/>的配置
但是在我们自己定义的struts.xml中有若干个<action/>的配置
因此abstract="true"表示当前包是抽象包,所以当前包中不允许出现<action/>的配置
抽象包的作用极类似于Java抽象类。自己不能实例化,只能被继承,然后由子类实现它的若干方法,最后由子类实例化
所以struts-default包中不能包含任何<action/>定义,具体的定义则是由其子包来定义的


<package name="struts2" extends="struts-default">中的extends的含义
Struts2中可以用包机制区分不同的Action,而将功能相似的Action放到一个package
类似于Java中包的机制,不同的是Struts2的包支持多继承,如下所示
<package name="struts2" extends="struts-default, jfreechart-default">

并且Struts2的package之间,又是可以实现继承关系的
继承之后就可以把被继承的package中的所有东西都拿来使用了
所以它就相当于将<package name="struts-default"/>的全部东西都继承过来了
于是才可以直接把struts-default.xml包中的所有结果类型、拦截器等都拿来供我们使用
如果我们的包不继承Struts2提供的struts-default包,那么我们很多工作都会变得无法完成
在Struts2应用启动时会自动读取struts-default.xml,将该文件定义的所有东西加载到内存中

接下来再加载当前应用的struts.xml文件,然后将内存中struts-default.xml的信息读取过来
然后才会加入到我们当前的应用中,并且在default.properties的第180行和181行有如下代码
### A list of configuration files automatically loaded by Struts
struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml
翻译
:被Struts自动加载的一个配置文件列表
         应用启动时加载顺序是:先加载struts-default.xml次之struts-plugin.xml最次struts.xml

因此:后者中的配置信息就可以覆盖掉前者中的配置信息


<package/>的namespace属性
若未配置<package/>标签的namespace属性,则相当于namespace="",表示当前package位于默认命名空间里
命名空间要以斜线开头,假设包的namespace="/hello",且表单action="login",然后在页面中点击提交按钮
这时在页面中将显示The requested resource (/struts2/login) is not available

这时可将表单改为action="/hello/login"或在表单中使用namespace和action属性分开来写
<form action="login" namespace="/hello">注意此时若写成login.action则会出错

修改完表单action后,再次提交时显示的却是The requested resource (/struts2/hello/login) is not available

这是因为根本就不存在这个路径,正确的路径应该是/struts2/hello/login.action

所以配置包的namespace后,须手工添加action后缀,即action="/hello/login.action"

再次提交则正常显示,且浏览器显示的是http://127.0.0.1:8088/struts2/hello/login.action

另外
:假设发送action="/test/login.action"请求,但struts.xml中根本就没有命名空间为test的包
当Struts2发现该命名空间不存在时,它会到默认命名空间下寻找login的<action/>的信息
假设发送action="/hello/login.action"请求,当/hello的namespace下不存在login.action时
Struts2会尝试到默认的命名空间下去寻找有没有名字叫做login的<action/>信息
若默认命名空间下不存在名为login的<action/>的信息,则会报告资源找不到的错误


Struts2还支持根命名空间【"/"】
当直接请求contextPath下的资源时,它会先到根命名空间下寻找匹配的Action
例如请求http://127.0.0.1:8088/project/BB.action时,Strtus2会首先到"/"命名空间下去找这个Action
服务器的根
http://127.0.0.1:8088/
网   站的根
http://127.0.0.1:8088/struts2_demo/
假设JSP中写成<a href=“/login.jsp”></a>,则实际指向的是http://127.0.0.1:8088/login.jsp

JSP中的根指的是服务器的根。而命名空间的斜杠代表网站的根,也叫项目的根或者应用的根


struts.xml中<action/>的name值的开头不允许加斜杠
这是由default.properties的第109行属性决定,即struts.enable.SlashesInActionNames默认为FALSE

比如说在struts.xml中,不指定<package/>的namespace属性,并写成<action name="hello/login">

再在表单中写成action="<%=request.getContextPath()%>/hello/login.action",此时仍可正常访问
但要注意:此时不能写成<action name="/hello/login"/>,否则在运行时仍会报告404错误

总之直接使用命名空间就是了,不建议修改struts.enable.SlashesInActionNames属性,保持默认配置即可

 


七、  Spring整合Struts2的两种方案(struts2,spring整合)

 

一、 需要的 JAR 文件为: SpringStruts2 框架本身需要的 JAR 文件以及他们所依赖的 JAR 文件,比如 commons-logging.jar 等等,另外还需要 Struts2 发布包中的 struts2-spring-plugin-x.xx.jar

二、 web.xml中增加 WebApplicationContext 的相应配置,以下两种配置方式本质是一样的。
1.  Servlet 2.3 及以上版本可以使用监听器,相应配置如下:

Xml代码  收藏代码
  1. <context-param>  
  2.               <param-name>contextConfigLocation</param-name>  
  3.               <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  
  4. </context-param>  
  5. <listener>  
  6.               <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  7. </listener>  

 

如果spring配置文件被命名为applicationContext.xml,并且放在WEB-INF目录下,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件。若存在多个Spring配置文件,则在<param-value>中依次列出,之间以逗号隔开。


2. Servlet 2.3 以下版本由于不支持<listener>,需要配置<servlet>,格式如下:

Xml代码  收藏代码
  1. <context-param>  
  2.               <param-name>contextConfigLocation</param-name>  
  3.               <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  
  4. </context-param>  
  5. <servlet>  
  6.               <servlet-name>contextLoaderServlet</servlet-name>  
  7.               <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  
  8.               <load-on-startup>1</load-on-startup>  
  9. </servlet>  

 

如果spring配置文件被命名为applicationContext.xml,并且放在WEB-INF目录下,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件,或者是名字为contextConfigLocationServletContext参数所指定的文件。由于该Servlet配置只是为了在容器启动时能启动ContextLoaderServlet使其工作,而不需要引用该Servlet,所以不需要配置<servlet-mapping>


三、web.xml 中完成加载 WebApplicationContext 之后,接下来就可以做到 Spring Struts2 的整合了。整合有两种方法,分别叙述如下:
1.  第一种实现方法:
1)   将 Struts 的业务逻辑控制器类配置在 Spring 的配置文件中,业务逻辑控制器中引用的业务类一并注入。注意,必须将业务逻辑控制器类配置为 scope=”prototype”!
示例如下:

Xml代码  收藏代码
  1. <bean id=”LoginAction” class=”yaso.struts.action.LoginAction”>  
  2.                      <property name=”loginDao” ref=”LoginDao”/>  
  3. </bean>  

 

2)     在 struts.xml 或者等效的 Struts2 配置文件中配置 Action 时,指定 <action> 的 class 属性为 Spring 配置文件中相应 bean 的 id 或者 name 值。示例如下:

Xml代码  收藏代码
  1. <action name=”LoginAction” class=”LoginAction”>  
  2.                      <result name=”success”>/index.jsp</result>  
  3. </action>  

 


2. 第二种实现方法:

1)    业务类在 Spring 配置文件中配置,业务逻辑控制器类不需要配置,Struts2 的 Action 像没有整合 Spring 之前一样配置,<action> 的 class 属性指定业务逻辑控制器类的全限定名。
2)     业务逻辑控制器类(action)中引用的业务类不需要自己去初始化,Struts2 的 Spring 插件会使用 bean 的自动装配将业务类注入进来,其实业务逻辑控制器也不是Struts2 创建的,而是 Struts2 的 Spring 插件创建的。默认情况下,插件使用 by name 的方式装配,可以通过增加 Struts2 常量来修改匹配方式:设置方式为:struts.objectFactory.spring.autoWire = typeName,可选的装配参数如下:
a)  name:等价于Spring配置中的autowire=”byName”,这是缺省值。
b)  type:等价于Spring配置中的autowire=”byType”。
c)  auto:等价于Spring配置中的autowire=”autodetect”。
d)  constructor:等价于Spring配置中的autowire=” constructor”。


四、 如果原先在Struts2中使用了多个object factory,则需要通过Struts2常量显式指定object factory,方式如下:struts.objectFactory = spring;如果没有使用多个object factory,这一步可以省略。


五、可以通过设增加Struts2常量来指定是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true。设置方式为:struts.objectFactory.spring.useClassCache = false。


六、 至此,完成了两种方式的整合。比较这两种整合方式,其本质是一样的。不同之处在于,使用第二种自动装配的方式时,由于没有在Spring中配

 

 

在web.xml
里面设置

 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
  <filter-mapping>    
      <filter-name>struts2</filter-name>    
     <url-pattern>*.action</url-pattern>    
    </filter-mapping>    
     
    <filter-mapping>    
      <filter-name>struts2</filter-name>    
      <url-pattern>*.jsp</url-pattern>    
    </filter-mapping> 
----------struts2的action请求设置

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   /WEB-INF/spring/application.xml,/WEB-INF/spring/applicationDao.xml
   ,/WEB-INF/spring/applicationAction.xml,/WEB-INF/spring/applicationService.xml
  </param-value>
 </context-param>
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

配置spring管理相关组件。。

例如:

web.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.     id="WebApp_ID" version="2.5">  
  7.     <display-name>unkou</display-name>  
  8.       
  9.     <filter>  
  10.         <filter-name>loginCheck</filter-name>  
  11.         <filter-class>  
  12.             jp.or.nhk.ssp.common.interceptor.LoginCheckInteceptor  
  13.         </filter-class>  
  14.     </filter>  
  15.       
  16.     <filter-mapping>  
  17.         <filter-name>loginCheck</filter-name>  
  18.         <url-pattern>/manual/*</url-pattern>  
  19.         <url-pattern>/guide/*</url-pattern>  
  20.     </filter-mapping>  
  21.       
  22.     <filter>  
  23.         <filter-name>struts2</filter-name>  
  24.         <filter-class>  
  25.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  26.         </filter-class>  
  27.     </filter>  
  28.   
  29.     <filter-mapping>  
  30.         <filter-name>struts2</filter-name>  
  31.         <url-pattern>/*</url-pattern>  
  32.     </filter-mapping>  
  33.   
  34.     <listener>  
  35.         <listener-class>  
  36.             org.springframework.web.util.Log4jConfigListener  
  37.         </listener-class>  
  38.     </listener>  
  39.   
  40.     <listener>  
  41.         <listener-class>  
  42.             org.springframework.web.context.ContextLoaderListener  
  43.         </listener-class>  
  44.     </listener>  
  45.   
  46.     <context-param>  
  47.         <param-name>contextConfigLocation</param-name>  
  48.         <param-value>  
  49.             /WEB-INF/spring-config/spring-context.xml  
  50.         </param-value>  
  51.     </context-param>  
  52.       
  53.     <context-param>  
  54.         <param-name>log4jConfigLocation</param-name>  
  55.         <param-value>file:/opt/ssp/sspu/conf/log4j.xml</param-value>  
  56.     </context-param>  
  57.       
  58.     <context-param>  
  59.         <param-name>log4jRefreshInterval</param-name>  
  60.         <param-value>60000</param-value>  
  61.     </context-param>  
  62.       
  63.     <context-param>  
  64.         <param-name>log4jExposeWebAppRoot</param-name>  
  65.         <param-value>false</param-value>  
  66.     </context-param>  
  67. </web-app> 
分类: java
标签: struts
绿色通道:好文要顶关注我收藏该文与我联系
justup
关注 - 0
粉丝 - 0
+加关注
0
0
(请您对文章做出评价)
«博主前一篇:perl正则表达式
»博主后一篇:初品cakephp

posted on 2011-11-18 09:05 justup 阅读(172) 评论(0)  编辑收藏

原创粉丝点击