当前位置: 代码迷 >> Java Web开发 >> Spring注入action为null,先多谢大家的帮助
  详细解决方案

Spring注入action为null,先多谢大家的帮助

热度:4835   发布时间:2016-04-10 22:30:25.0
Spring注入action为null,先谢谢大家的帮助
struts代码

<?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="*" extends="struts-default" namespace="*">
<global-results>
<result name="error">/error.jsp </result>
</global-results>
</package>
<package name="default" namespace="/" extends="struts-default">
<action name="*Form" class="com.gemway.test.action.CheckFormAction" method="{1}">
<result>index.jsp</result>
<result name="act" >index.jsp</result>
</action>
</package>



</struts>



action代码



/**  

* @Title: CheckFormAction.java 

* @Package com.gemway.test.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author  

* @date 2014-5-5 上午10:51:46 

* @version V1.0  

*/ 
package com.gemway.test.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.gemway.test.bean.UserBean;
import com.gemway.test.service.CheckFormService;
import com.gemway.test.service.impl.CheckFormServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

/**   
 *    
 * 项目名称:SSHproject   
 * 类名称:CheckFormAction   
 * 类描述:   
 * 创建人:   
 * 创建时间:2014-5-5 上午10:51:46   
 * 修改人:   
 * 修改时间:2014-5-5 上午10:51:46   
 * 修改备注:   
 * @version    
 *    
 */

public class CheckFormAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private UserBean userBean;
public UserBean getUserBean() {
return userBean;
}

public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}


// CheckFormService checkFormService=new CheckFormServiceImpl();
CheckFormService checkFormService;

public CheckFormService getCheckFormService() {
return checkFormService;
}

public void setCheckFormService(CheckFormService checkFormService) {
this.checkFormService = checkFormService;
}

@Override
public String execute() throws Exception {
System.out.println("execute");

return null;
}

public String check() throws Exception {
try {


HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
System.out.println("check");
String name=userBean.getUsrAccount();
String password=userBean.getUsrPassword();
System.out.println(name);
System.out.println(password);
userBean=checkFormService.check(name, password); //此处的checkFormService为null 所以没有注入成功
request.setAttribute("userBean", userBean);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("action出错");
}
return "act";
}



}



spring配置文件


 <bean id="checkFormAction" class="com.gemway.test.action.CheckFormAction" scope="prototype">
     <property name="checkFormService" ref="checkFormService" />
 </bean>

<bean id="checkFormDao" class="com.gemway.test.dao.impl.CheckFormDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="checkFormService" class="com.gemway.test.service.impl.CheckFormServiceImpl">
     <property name="checkFormDao" ref="checkFormDao" />
 </bean>




控制台是报空指针
java.lang.NullPointerException
at com.gemway.test.action.CheckFormAction.check(CheckFormAction.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
  相关解决方案