spring +struts2 中set注入问题求高手指点怎么老是出错...这个是接口 UserInterFace 的方法:public boolea
spring +struts2 中set注入问题求高手指点怎么老是出错... 这个是接口 UserInterFace 的方法: public boolean loGin(String userName,String userPwd); 这个是接口的实现类UserFunction 方法: /**用户登录方法**/ public boolean loGin(String userName,String userPwd){ boolean scu=false; try { ConnDB conn=new ConnDB(); String sql="select userpwd from userTB where username='"+userName+"'"; ResultSet rs= conn.executeQuery(sql); if(rs.next()){ String password =rs.getString(1); if(userPwd.equals(password)){ scu=true; } } conn.close(); } catch(Exception e){e.printStackTrace();} return scu; } 这个是spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="ConnDB" class="org.com.wzy.clzy.db.ConnDB"></bean> <bean id="UserFunction" class="org.com.wzy.clzy.function.user.UserFunction"></bean> <bean id="LoginAction" class="org.com.wzy.clzy.actions.login.LoginAction"> <property name="uf" ref="UserFunction"/> </bean> </beans> 提示的错误就是: java.lang.NullPointerException org.com.wzy.clzy.actions.login.LoginAction.yzlogin(LoginAction.java:38) 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:453) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255) org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) 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:249) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)[最优解释] 把你的struts的配置文件贴出来,和你的loginAction的属性[其他解释]
org.com.wzy.clzy.actions.login.LoginAction.yzlogin(LoginAction.java:38)查找错误信息,再仔细找找
[其他解释] 空了。返回的值 有问题啊
------其他解决方案--------------------
空指针错误,首先就需要判断是哪个值为空?
你可以把你任务有可能的都system.out.println一下,看看哪个为空?
然后一步一步的来,
我感觉凡是这种报空指针的错误的,其实自己都是能解决的。
[其他解释] 看看你的web.xml,先保证你的spring的配置文件加载了再说吧,估计是没有加载的
[其他解释] <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
你指定一下你的spring配置文件的路径吧
[其他解释] 既然是struts2 action为什么要继承 ActionSupport
UserInterFace 接口的实现方法在spring里面配置了吗?
dao配置了吗
[其他解释] 已经在Action中给了UserFunction的属性,并给它setter方法了,
为什么还用:
ApplicationContext context=new ClassPathXmlApplicationContext(new String[] {"beans.xml"});
UserInterFace userf=(UserInterFace)context.getBean("UserFunction");
来获取id = UserFunction 的bean?不是会通过spring的XML配置文件依赖注入的吗?
[其他解释] 引用: 引用:既然是struts2 action为什么要继承 ActionSupport UserInterFace接口的实现方法在spring里面配置了吗? dao配置了吗 不继承ActionSupport我怎么调用业务呀,UserInterFace接口的实现我肯定会在spring里面配置啦. 先了解struts2和1的区别
然后好好看看SSH中spring的原理
[其他解释] 我的struts文件时零配置,注解方式,我的action就是这样写的
package org.com.wzy.clzy.actions.login;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.com.wzy.clzy.interfase.user.UserInterFace;
import org.com.wzy.clzy.model.UserModel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private UserInterFace uf;
public void setUf(UserInterFace uf) {
this.uf = uf;
}
public UserInterFace getUf() {
return uf;
}
private UserModel user;
public UserModel getUser() {
return user;
}
public void setUser(UserModel user) {
this.user = user;
}
/**
* 从写父类的方法!
* */
@Action(
value="log1",
results={
@Result(name="success",location="main/index",type="redirect"),
@Result(name="error",location="main/error",type="redirect")
})
public String yzlogin() {
boolean src=false;
System.out.println(user.getUsername()+"^^^^^^^^^^^^^^^^"+ user.getUserpwd());
System.out.println(this.uf);
try{
ApplicationContext context=new ClassPathXmlApplicationContext(new String[] {"beans.xml"});
UserInterFace userf=(UserInterFace)context.getBean("UserFunction");
System.out.println("运行到站了"+userf);
}catch(Exception e){e.printStackTrace();}
//if(src==true){
return SUCCESS;
//}else{ //return ERROR; //} } }[其他解释] spring的话我这样就可以获取bean实例 ApplicationContext context=new ClassPathXmlApplicationContext(new String[] {"beans.xml"}); UserInterFace userf=(UserInterFace)context.getBean("UserFunction"); 但是用set注入的话获取的bean是空指针[其他解释] org.com.wzy.clzy.actions.login.LoginAction.yzlogin(LoginAction.java:38) 这个方法是没有错的,就是set注入的时候获取不到实现类yzlogin的实例[其他解释]
引用: 看看你的web.xml,先保证你的spring的配置文件加载了再说吧,估计是没有加载的 这个加载了的,我都换了好几种加载方法.这个是web.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>clzycht</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[其他解释] 引用: 既然是struts2 action为什么要继承 ActionSupport UserInterFace接口的实现方法在spring里面配置了吗? dao配置了吗 不继承ActionSupport我怎么调用业务呀,UserInterFace接口的实现我肯定会在spring里面配置啦.
[其他解释] 引用: 空指针错误,首先就需要判断是哪个值为空? 你可以把你任务有可能的都system.out.println一下,看看哪个为空? 然后一步一步的来, 我感觉凡是这种报空指针的错误的,其实自己都是能解决的。 我我调用的那个实例bean是空值前面action里面private UserInterFace uf;
public void setUf(UserInterFace uf) {
this.uf = uf;
}
这个值是空的...
[其他解释] 这个问题我换了一种方法已经解决了,我用了spring的零配置那注入bean做了使用配置文件来注入就会报错,只是想知道我用配置文件错在哪里。。非常感谢大家为我解答...