java.lang.NullPointerException&com.cb.action.LoginAction.execute(LoginAction.java:48)
今天做一个Spring和Struts的融合,通过bean注入后,程序一跑起来,就报这个错误:
java.lang.NullPointerException com.cb.action.LoginAction.execute(LoginAction.java:48) 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:452) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
下面先交待下各配置文件的情况:
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> <package name="login2" extends="struts-default" > <action name="login" class="loginAction"> <result name="error">/err.jsp</result> <result name="success">/welcome.jsp</result> </action> <!-- 让用户直接访问该应用时列出所有视图页面 --> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
Spring的applicationContext.xml如下:
<?xml version="1.0" encoding="GBK"?> <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="myService" class="com.cb.serviceImpl.MyServiceImpl"> </bean> <!-- controller --> <bean id="loginAction" class="com.cb.action.LoginAction" scope="prototype"> <property name="ms" ref="myService"></property> </bean> <!-- <bean id="ms" class="com.cb.serviceImpl.MyServiceImpl"/> --> </beans>
唯一的action如下,LoginAction.java
package com.cb.action; import com.cb.serviceInter.MyService; import com.opensymphony.xwork2.Action; public class LoginAction implements Action{ private String username; private String password; private String tip; //系统所需业务逻辑组件 private MyService ms; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } public MyService getMs() { return ms; } public void setMs(MyService ms) { this.ms = ms; } public String execute() throws Exception { System.out.println("************test1****"+ms+"名字是:"+getUsername()); if(ms.valid(getUsername(), getPassword())){ setTip("恭喜,整合成功!"); return SUCCESS; }else { setTip("对不起,登录失败!"); return ERROR; } } }【分析】通过报的错误,可以明确的知道,private MyService ms;没有正常注入,就是这个实例是null。显然是spring的注入出现问题,但是我检查了所有的情况,发现ms的setMs也有,配置的也没有任何问题,这就奇怪了,怎么回一直提示注入失败呢?
【解决】最后发现一切问题出在struts.xml中,有两种解决办法:1,在<package name="login2" extends="struts-default" >中加上namespace=“/”。2,把action的<action name="login" class="loginAction">中name=“login”改成别的任何名字,估计有可能是login为struts2的保留字,所以以后的命名,尽量不要使用login,换个别的名字。但是第一种方法,加上namespace=“/”我始终没想明白是什么原因,加上命名空间了以后就没有错误,因为命名空间是一个逻辑名,访问action的时候,我不加逻辑名,应该不会有问题的。还是没想明白,希望各位看出问题的,能交流下,万分感谢!