这个问题已经困扰好几天了,检查了无数遍依旧不知道哪里出现了错误,求大神们指点迷津,在此拜谢。
action:
public class DailyManagerAction extends ActionSupport implements RequestAware { private Map<String,Object> request; private DailyManagerService dailyManagerService; private Emp emp; private List<Daily> list; public String execute() throws Exception{ System.out.println("进入DailyManagerAction+s"); System.out.println("emp_id"+emp.getEmp_id()); list=this.dailyManagerService.selecAllDaily(emp.getEmp_id()); System.out.println(list.size()); request.put("daily", list); return SUCCESS; } public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this.request=request; } public void setEmp(Emp emp) { this.emp = emp; } public Emp getEmp() { return emp; } public List<Daily> getList() { return list; } public void setList(List<Daily> list) { this.list = list; } public void setDailyManagerService(DailyManagerService dailyManagerService) { this.dailyManagerService = dailyManagerService; } }
service层:
public class DailyManagerServiceImpl implements DailyManagerService { private DailyManagerDao dailyManagerDao; public void setDailyManagerDao(DailyManagerDao dailyManagerDao) { this.dailyManagerDao = dailyManagerDao; } public List<Daily> selecAllDaily(int emp_id) { System.out.println("进入DailyManagerServiceImpl"); return this.dailyManagerDao.selecAllDaily(emp_id); }
bean的配置文件:
<bean id="login" class="com.neusoft.action.LoginAction" scope="prototype"> <property name="loginService" ref="loginServiceImpl"></property> </bean> <bean id="loginServiceImpl" class="com.neusoft.service.LoginServiceImpl"> <property name="loginDao" ref="loginDaoImpl"></property> </bean> <bean id="loginDaoImpl" class="com.neusoft.dao.LoginDaoImpl"> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> <bean id="dailyManagerDaoImpl" class="com.neusoft.dao.DailyManagerDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="dailyManagerServiceImpl" class="com.neusoft.service.DailyManagerServiceImpl"> <property name="dailyManagerDao" ref="dailyManagerDaoImpl"></property> </bean> <bean id="selectDailyByDailyId" class="com.neusoft.action.SelectDailyByDailyIdAction" scope="prototype"> <property name="dailyManagerService" ref="dailyManagerServiceImpl"></property> </bean> <bean id="updateDailyByDailyId" class="com.neusoft.action.UpdateDailyByDailyIdAction" scope="prototype"> <property name="dailyManagerService" ref="dailyManagerServiceImpl"></property> </bean> <bean id="delDailyByDailyId" class="com.neusoft.action.DelDailyByDailyId" scope="prototype"> <property name="dailyManagerService" ref="dailyManagerServiceImpl"></property> </bean> <bean id="gotoAddDaily" class="com.neusoft.action.GotoAddDailyAction" scope="prototype"> </bean> <bean id="dailyManager" class="com.neusoft.action.DailyManagerAction" scope="prototype"> <property name="dailyManagerService" ref="dailyManagerServiceImpl"></property> </bean> <bean id="addDaily" class="com.neusoft.action.AddDailyByEmpIdAction" scope="prototype"> <property name="dailyManagerService" ref="dailyManagerServiceImpl"></property> </bean>
报错:
com.neusoft.action.DailyManagerAction.execute(DailyManagerAction.java:22) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
输出:
进入DailyManagerAction+s
emp_id12
解释:
1、DailyManagerAction.java:22是指:
list=this.dailyManagerService.selecAllDaily(emp.getEmp_id());这行
2、没有输出service层的:进入DailyManagerServiceImpl
所以推测:没有进入service层
3、但是其他的action也是用的相同的dailyManagerService这个依赖注入的属性,都能够得到正确的输出,仅是在这一个action里出现空指针错误。
小弟刚学ssh,有太多的东西不会,但这个问题搜了太多太多的答案,还是没有解决,望路过的大神们能够帮忙,再次拜谢。
private DailyManagerService dailyManagerService;这句改为:
private DailyManagerService dailyManagerServiceImpl;
非常感谢,终于测试成功了,但原理有些不懂,为什么用dailyManagerServiceImpl而不用daiyManagerService,大神能指点一下吗。
@风间谷: 很简单,他是按照bean的id注入的,你写的daiyManagerService他就会在配置文件中找id为daiyManagerService的bean,但是找不到,就注入不成功
@飞龙在天001:
谢谢,但其他的许多action都是用dailyManagerService且可以注册成功,
@风间谷: 你可能要看下spring的配置文件。其他使用Service接口的Action的配置文件中bean的id是否为接口名称
dailyManagerService注入成功了吗
应该注册成功了,许许多多的action都是通用这一个dailyManagerService,其他的action的调用都成功,所以推测dailyManagerService注入成功
@风间谷: 在System.out.println("emp_id"+emp.getEmp_id());后面添加测试输出System.out.println(this.dailyManagerService == null);
@jello chen:
刚测试了一下,输出结果为:true,dailyManangerService没有注册成功,但和其他action里的应用方式一模一样,不知道哪里出现了问题,大神帮我看一下bean里有没有错误,(这种测试方法没有考虑到,跪谢)
@风间谷: 一时也没看出问题,建议在setDailyManagerService中做个测试输出
@jello chen:
发现根本没有进入这个方法,这是什么原因