除了execute和validate方法之外,Spring Bean抛出NullPointerException

时间:2014-10-13 04:09:56

标签: java spring jsp struts2 dependency-injection

我将Spring与Struts2集成在一起。我使用bean来创建会话工厂的会话。我的代码如下。如果我在执行中使用bean或者验证我的方法是有效的,那么它会抛出NullPointerException

行动类:

public class GetEmp extends ActionSupport {
  private spDao myDao;
  public String execute(){
    User user=myDao.getDbsession().get(User.class);
  }
} 

工作正常。如果我在其他类中使用相同的bean而不扩展ActionSupport抛出NullPointerException

applicationContext.xml

<?xml version="1.0" encoding="windows-1252"?>
<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.5.xsd">

    <bean id="myDao" class="controller.spDAO" scope="request" /> 
    <bean id="sendMail" class="controller.Emailfunction" scope="request"/>

</beans>

Java类:

public class GetEmp {
  private spDao myDao;
  public String getEmpname(){
    User user=myDao.getDbsession().get(User.class);
  }
} 

抛出NullPointerException。如何解决这个问题?

web.xml

<?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">
    <display-name>Matrimonial</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
     <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>503</error-code>
        <location>/503.jsp</location>
    </error-page>
</web-app>

我通过使用方法传递我的bean名称来解决这个问题。他们是否有其他解决方案?

任何帮助将不胜感激!!

1 个答案:

答案 0 :(得分:1)

另一个解决方案是如果你使用spring-plugin来创建Spring的动作bean,并配置要注入的依赖

<bean id="getEmp" class="controller.GetEmp" scope="request"> 
    <property name="myDao" ref="myDao" />     
</bean>

struts.xml你应该有一些东西

<action name="getEmp" class="getEmp" method="getEmpname"/>

详细了解Struts2-Spring用法和解释。