spring3 struts2 整合

轉處:http://cuizhihua.iteye.com/blog/1424283


今天同事在搭ssh框架,遇到了一個很奇怪的問題,他按照網上的一些資料,配好了,但是在action裏面就是拿不到spring注入的service層的對象,經過了幾個小時的奮鬥終於搞定。

首先列出我修改之前的配置信息:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="
http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
  <!-- 指定log4j.properties配置文件的位置 -->
 <context-param> 
  <param-name>log4jConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/log4j.properties</param-value> 
 </context-param>
 <!-- 指定以Listener方式啓動Spring容器 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener> 
 
 <!-- 指定以Listener方式啓動Log4j -->
 <listener> 
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
 </listener>
 <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>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

 

 struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <!-- 設置Struts2.1應用是否處於開發模式,通常在開發調試階段設爲true,正式上線後可設爲false -->
 <constant name="struts.devMode" value="true" />
 <!-- 設置Struts2.1默認的ObjectFactory爲  org.apache.struts2.spring.StrutsSpringObjectFactory-->
 <constant name="struts.objectFactory" value="spring" />
 <!--<constant name="struts.objectFactory.spring.autoWire" value="name" />-->
 
 
 <package name="test"  extends="struts-default">
  <action name="login" class="loginAction" method="execute">
   <result name="success">/success.jsp</result>
   <result name="error">/fail.jsp</result>
  </action>
 </package>
</struts>   

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >


 <bean id="userDAO" class="com.ssh.test.dao.UserDao">
  <bean id="loginCheck1" class="com.ssh.test.service.LoginCheck">
   <property name="userDao" ref="userDAO"/>
  </bean>
  <bean id="loginAction" class="com.ssh.test.action.LoginAction" >
   <property name="loginCheck" ref="loginCheck1"/>
  </bean>
  </beans>

login.jsp

  <body>
    <s:form action="/login.action" >
     dd<s:textfield name="user.userName" label="UserName"/>
     <s:password name="user.password" label="Password"/>
     <s:submit value="Login"/>
    </s:form>
  </body>

 

經過反覆推敲琢磨,通過日誌打印可以發現,其實spring已經實例化類loginAction並且也進行了loginCheck1這個業務對象的注入,可是爲什麼從頁面過來的請求,就是取不到這個loginCheck1呢?

原因在於<package name="test" namespace="/" extends="struts-default">少寫了這個namespace="/".

 

總結:

 

struts2 爲了提高效率而引入了namespace,這個namespace能夠使頁面過來的請求儘快的找到相對應的action;如果上面寫了這個namespace strtus2就會調用spring生成的那個loginAction,如果什麼都不寫struts框架就會生產出一個新的loginAction,沒有人爲這個新的loginAction注入loginCheck1業務層對象,所以在這種情況下struts就拿不到業務層的對象loginCheck1對象了。

順便提一下:

struts2的action是非單例的,這也就是爲什麼action是有狀態的原因;而在spring中默認的配置的bean都是單例的,所以在配置applicationContext.xml文件時尤其是讓spring來管理action的生命週期時要注意!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章