spring3 struts2 整合

4 篇文章 0 订阅
2 篇文章 0 订阅

转处: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的生命周期时要注意!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值