spring3 struts2 整合

原创文章,转载请表明来源。

今天同事在搭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的生命周期时要注意!

猜你喜欢

转载自cuizhihua.iteye.com/blog/1424283