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没有被注入,问题一直解决不了
@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没有被注入,问题一直解决不了
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at com.action.UserAction.execute(UserAction.java:31)
private UserManager userManager;
你好,还是同样的错误,出错位置if(userManager.existUser(user)){
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
把配置l文件全贴出来看看
顺便看一下有没有把struts-spring-plugin.jar包加进来
我以前也遇到过这样问题,配置下配置文件就行了
你好,我把源码传上来了,帮忙看一下,下载地址:http://download.csdn.net/detail/kxf327/4055771
不胜感激!
你好,我把源码传上来了,帮忙看一下,下载地址:http://download.csdn.net/detail/kxf327/4055771
不胜感激!
<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>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springContext/applicationContext.xml</param-value>
</context-param>
搞了半天是路径错误