Spring과 Struts2가 통합되었습니다. 나는 세션 팩토리에서 세션을 생성하기 위해 bean을 사용한다. 내 코드는 아래와 같습니다. 실행에 Bean을 사용하거나 메소드가 작동하는지 확인하면 다른 곳에서는 NullPointerException
을 던집니다.Spring Bean이 메소드 실행 및 유효성 검사 이외의 NullPointerException을 던졌습니다.
액션 클래스 :
public class GetEmp extends ActionSupport {
private spDao myDao;
public String execute(){
User user=myDao.getDbsession().get(User.class);
}
}
그것은 잘 작동합니다. ActionSupport
을 확장하지 않는 다른 클래스에서 동일한 bean을 사용하는 경우 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>
자바 클래스 :
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>
나는 방법 내 빈 이름을 전달하여이 문제를 처리했다. 다른 해결책이 있습니까?
도움이 될 것입니다! 당신이 struts.xml
당신이 뭔가해야
<action name="getEmp" class="getEmp" method="getEmpname"/>
읽기에서
<bean id="getEmp" class="controller.GetEmp" scope="request">
<property name="myDao" ref="myDao" />
</bean>
를 주입하는 의존성을 스프링에 의해 액션 콩을 작성하고 구성하는 스프링 플러그인을 사용하는 경우
null 포인터 예외는 어디서 발생 했습니까? – gerrytan
@ gerrytan, myDao.getDbsession() 호출시. – Madhesh
당신이 당신의 빈을 정의한 환경 설정 파일을 보여줄 수 있습니까? 그리고 액션 클래스에 주입 된 의존성이 있습니다. –