兄弟们讨教一个问题 ,JDK1.7 Myelipse Tomcat 7 spring3.24,跟的是一个SSH的OA项目, struts2的一个action文件之前还跑得好好的 ,跳转都无问题, 现在排查出来了是这一句代码突然报空指针错误, 而且导致struts2的action跳转都失效了,用的是spring注解,spring接管struts2。前几天还用的好好的 突然就出现这个情况了 而且莫名其妙不知道问题出现在哪,代码和教程完全一致,求各位指点!
其中下面这段RoleAction代码的第二行代码roleList = roleService.findAll();有空指针错误
其中下面这段RoleAction代码的第二行代码roleList = roleService.findAll();有空指针错误
public String list() throws Exception{
List<Role> roleList = new ArrayList<Role>();
roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList); //放入值栈
return "list";
}
完整代码如下,roleService通过spring注解注入注入
package cn.itcast.oa.view.action; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.itcast.oa.domain.Role; import cn.itcast.oa.service.RoleService; import cn.itcast.oa.service.impl.RoleServiceImpl; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @Controller("roleAction") @Scope("prototype") public class RoleAction extends ActionSupport implements ModelDriven<Role> { @Resource RoleService roleService; //把前台的各种属性通过实现ModelDriven接口封装在Role类中 通过实现getModel方法返回即可 前台直接写属性名 private Role model = new Role(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } private Long id; private String name; private String description; //列表 public String list() throws Exception{ List<Role> roleList = new ArrayList<Role>(); roleList = roleService.findAll(); ActionContext.getContext().put("roleList", roleList); //放入值栈 return "list"; } public RoleService getRoleService() { return roleService; } public void setRoleService(RoleService roleService) { this.roleService = roleService; } //删除 public String delete() throws Exception{ roleService.delete(id); return "toList"; } //添加页面 public String addUI() throws Exception{ return "addUI"; } public String add() throws Exception{ //封装到对象中 Role role = new Role(); role.setName(name); role.setDescription(description); //保存到数据库 roleService.save(role); return "toList"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String editUI() throws Exception{ Role role = roleService.getById(id); ActionContext.getContext().getValueStack().push(role);// 可以放入值栈或用下面的方法 return "editUI"; } public String edit() throws Exception{ Role role = roleService.getById(id); role.setName(name); role.setDescription(description); roleService.update(role); return "toList"; } public String test() throws Exception{ return "test"; } @Override public Role getModel() { return model; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 配置扩展名为action -->
<constant name="struts.action.extension=action" value="true" />
<!-- 主题配置为simple -->
<constant name="struts.ui.theme=xhtml" value="simple"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
<!-- 当Struts2与Spring整合后,class属性可以写bean的名称 -->
<!-- <action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action> -->
<!-- 岗位管理 -->
<action name="role_*" class="cn.itcast.oa.view.action.RoleAction" method="{1}">
<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
<result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
<result name="toList" type="redirectAction">role_list</result>
<result name="test">/test.jsp</result>
</action>
</package>
</struts>
applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
<context:annotation-config />
<!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- <bean id="roleAction" class="cn.itcast.oa.test" scope="prototype" /> -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0数据库连接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。假如maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
<!-- 配置声明式事务管理(采用注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
异常
Struts Problem Report Struts has detected an unhandled exception: Messages: File: cn/itcast/oa/view/action/RoleAction.java Line number: 47 Stacktraces java.lang.NullPointerException cn.itcast.oa.view.action.RoleAction.list(RoleAction.java:47) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
解决方案
20
查看下struts-default.xml中这个配置是不是已经默认设置为spring,你这个问题不是struts的action没有实例化,而是action没有交给spring管理,所以你无法注入