当前位置: 代码迷 >> J2EE >> struts2.21+spring2.5+hibernate3.2,求解依赖注入空指针(来者有分),该怎么解决
  详细解决方案

struts2.21+spring2.5+hibernate3.2,求解依赖注入空指针(来者有分),该怎么解决

热度:302   发布时间:2016-04-22 01:40:26.0
struts2.21+spring2.5+hibernate3.2,求解依赖注入空指针(来者有分)
-------------------Struts---------------------
Java code
<?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.objectFactory" value="spring" />      <package name="struts2"  extends="struts-default">          <global-results>                  <result name="error">/login.jsp</result>          </global-results>          <action name="login" class="LoginAction" method="doLogin">                      <result name="success">/index.jsp</result>                       <result name="error">/login.jsp</result>          </action>      </package>  </struts>


-------------------------applicationContext.xml---------------------------
Java code
<bean id="UserBasicInfoDAO" class="model.UserBasicInfoDAO">        <property name="sessionFactory">            <ref bean="sf" />        </property>    </bean>        <bean id="LoginImpl" class="impl.LoginImpl" >        <property name="userBasicInfoDAO" ref="UserBasicInfoDAO" />    </bean>        <bean id="LoginAction" class="action.LoginAction" >        <property name="loginImpl" ref="LoginImpl" />    </bean>


-----------------------------------web.xml----------------------
Java code
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>    </context-param>        <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>         <filter>        <filter-name>ssh2</filter-name>        <filter-class>             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter            <!-- org.apache.struts2.dispatcher.FilterDispatcher-->        </filter-class>    </filter>    <filter-mapping>        <filter-name>ssh2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    </web-app>----------------------------LoginAction----------------------------package action;import model.UserBasicInfo;import impl.LoginImpl;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{        private UserBasicInfo userBasicInfo;        private LoginImpl loginImpl;        public LoginImpl getLoginImpl() {        return loginImpl;    }    public void setLoginImpl(LoginImpl loginImpl) {        System.out.println("set LoginImpl");        this.loginImpl = loginImpl;    }    public String doLogin() throws Exception {        // TODO Auto-generated method stub        System.out.println("userId-------------"+userBasicInfo.getUserId());        UserBasicInfo userBasicInfo1=loginImpl.getUserBasicInfo(userBasicInfo.getUserId());//loginImpl空指针        if(userBasicInfo1!=null)        {            System.out.println("登录成功");            return SUCCESS;        }else        {            System.out.println("登录失败");            return ERROR;        }    }    public UserBasicInfo getUserBasicInfo() {        return userBasicInfo;    }    public void setUserBasicInfo(UserBasicInfo userBasicInfo) {        this.userBasicInfo = userBasicInfo;    }}---------------------------------LoginImpl----------------------------[code=Java]package impl;import model.UserBasicInfo;import model.UserBasicInfoDAO;public class LoginImpl {    private UserBasicInfoDAO userBasicInfoDAO;    public UserBasicInfoDAO getUserBasicInfoDAO() {        return userBasicInfoDAO;    }    public void setUserBasicInfoDAO(UserBasicInfoDAO userBasicInfoDAO) {        System.out.println("set userbasicinfodao");        this.userBasicInfoDAO = userBasicInfoDAO;    }        public UserBasicInfo getUserBasicInfo(int userId)    {        return userBasicInfoDAO.findById(userId);    }}
  相关解决方案