struts2 和 spring 整合的问题

kxf327 2011-12-16 11:41:40
action层的代码
@Component("user")
public class UserAction extends ActionSupport {
private UserManager userManager;
private String username;
private String password;

public UserManager getUserManager() {
return userManager;
}
@Resource
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}

@Override
public String execute() throws Exception {
User user = new User();
user.setUsername(username);
user.setPassword(password);
if(userManager.existUser(user)){
return "fail";
}
userManager.add(user);
return "success";
}
......
}

service层中的代码
@Component("userManager")
public class UserManager {
private UserDao userDao;
private UserLogDao userLogDao;
...}

spring配置文件中使用
<context:annotation-config />
<context:component-scan base-package="com" />

--------------------------------------------------------------------------------
单元测试service层(只是spring)没报错
测试struts2时报错,类型为空指针
位置为action层中的
if(userManager.existUser(user)){
--------------------------------------------------------------------------------
我总怀疑struts和spring没有连接起来,userManager没有被注入,问题一直解决不了
...全文
221 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
kxf327 2012-07-17
  • 打赏
  • 举报
回复
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springContext/applicationContext.xml</param-value>
</context-param>
搞了半天是路径错误
kxf327 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 zhanghs886 的回复:]

spring的配置文件也贴出来看看……
[/Quote]
<?xml version="1.0" encoding="UTF-8"?>

<context:annotation-config />
<context:component-scan base-package="com" />

<bean id="logIntercepter" class="com.aop.LogInterceptor"></bean>
<!-- 日志管理AOP -->
<aop:config>
<aop:aspect id="logAspect" ref="logIntercepter">
<aop:before method="before" pointcut="execution(* com.service..*.add(..))" />
<aop:after method="after" pointcut="execution(* com.service..*.add(..))" />
</aop:aspect>
</aop:config>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/spring">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="10"></property>
<property name="maxWait" value="10"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>

<!-- <property name="annotatedClasses"> <list> <value>com.model.User</value>
<value>com.model.Userlog</value> </list> </property> -->

<property name="packagesToScan">
<list>
<value>com.model</value>
<!-- <value>com.model.*</value> -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop> <!-- 显示sql语句 -->
</props>
</property>
</bean>


<!-- 事务管理 -->
<!-- <tx:annotation-driven transaction-manager="txManager"/> -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- XML方式事务配置 -->
<aop:config>
<aop:pointcut expression="execution(public * com.service.*.*(..))"
id="businessService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="getUser" read-only="true" />
<!-- <tx:method name="exist*" read-only="true" /> -->
<tx:method name="add*" /> <!-- 默认propagation是REQUIRED -->
</tx:attributes>
</tx:advice>
zhanghs886 2012-02-10
  • 打赏
  • 举报
回复
spring的配置文件也贴出来看看……
kxf327 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qazxsw5814 的回复:]

遇到了同样的问题 求解
[/Quote]

现在你的解决了吗?我咨询了好的人都没有解决,苦啊
kxf327 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xiezhengao 的回复:]

配置文件中设置了“事务bean”没啊?
我以前也遇到过这样问题,配置下配置文件就行了
[/Quote]

你好,我把源码传上来了,帮忙看一下,下载地址:http://download.csdn.net/detail/kxf327/4055771
不胜感激!
kxf327 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 likefire 的回复:]

缺的东西太多了,还是找个例子好好看看吧
[/Quote]

你好,我把源码传上来了,帮忙看一下,下载地址:http://download.csdn.net/detail/kxf327/4055771
不胜感激!
西部流云 2011-12-24
  • 打赏
  • 举报
回复
缺的东西太多了,还是找个例子好好看看吧
dokia123 2011-12-23
  • 打赏
  • 举报
回复
自动注入的话 要是接口吧。UserManager要是接口,然后写一个他的实现类。
xiezhengao 2011-12-22
  • 打赏
  • 举报
回复
配置文件中设置了“事务bean”没啊?
我以前也遇到过这样问题,配置下配置文件就行了
QAZXSW5814 2011-12-20
  • 打赏
  • 举报
回复
遇到了同样的问题 求解
Lua598 2011-12-19
  • 打赏
  • 举报
回复
空指针。
未敢邀明月 2011-12-19
  • 打赏
  • 举报
回复
setUserManager的问题吧,好像声明一个 UserManager的变量加上源数据@Resource就ok了 不用在set get了,当然如果前后台传递的数据还是需要set get的
hfei99999 2011-12-19
  • 打赏
  • 举报
回复
没有下方了吗?等待解决方案!!!!
heboucheng 2011-12-18
  • 打赏
  • 举报
回复
肯定没有注入进来
把配置l文件全贴出来看看
顺便看一下有没有把struts-spring-plugin.jar包加进来
warriorLv 2011-12-18
  • 打赏
  • 举报
回复
你是否配置了Spring容器,在web.xml中配置如下
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
kxf327 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gbyseo 的回复:]

把错误贴出来,不贴怎么看啊。
[/Quote]
你好,sorry,

错误为:
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at com.action.UserAction.execute(UserAction.java:31)
Lua598 2011-12-16
  • 打赏
  • 举报
回复
把错误贴出来,不贴怎么看啊。
kxf327 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hgj1024454508 的回复:]

@Autowired
private UserManager userManager;
[/Quote]

你好,还是同样的错误,出错位置if(userManager.existUser(user)){
敬敬11 2011-12-16
  • 打赏
  • 举报
回复
@Autowired
private UserManager userManager;

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧