2012-05-02 36 views
1

的Tomcat 7,JSF 2,彈簧3,爪哇6的NullPointerException注入彈簧服務bean來JSF ManagedBean當

錯誤:userService.checkUser(getLogin())的NullPointerException(userService爲null)在UserBean.java存取時jsf頁面。

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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" 
      version="2.5"> 

    <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> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

faces-config.xml中

<faces-config 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-facesconfig_2_0.xsd" 
       version="2.0"> 
    <application> 
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
     <resource-bundle> 
      <base-name>i18n</base-name> 
      <var>msg</var> 
     </resource-bundle> 
     <locale-config> 
      <default-locale>en</default-locale> 
      <supported-locale>ru</supported-locale> 
     </locale-config> 
    </application> 
</faces-config> 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- Services Beans --> 
    <bean id="userService" class="service.UserServiceImpl"> 
     <property name="userDao" ref="userDao"/> 
    </bean> 

    <!-- DAOs --> 
    <bean id="userDao" class="dao.UserDaoImpl"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <!-- Hibernate session factory --> 
    <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="configLocation"> 
      <value>classpath:/hibernate.cfg.xml</value> 
     </property> 
    </bean> 

    <tx:annotation-driven/> 
    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
</beans> 

UsersBean.java

package beans; 

import service.UserService; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ManagedProperty; 
import javax.faces.bean.SessionScoped; 
import java.io.Serializable; 

@ManagedBean(name="usersBean") 
@SessionScoped 
public class UsersBean implements Serializable{ 
    private String login; 
    private String password; 
    @ManagedProperty(name = "userService", value = "#{userService}") 
    private UserService userService; 

    ...Getters, setters for every field... 

    public void checkRegistred(){ 
     userService.checkUser(getLogin()); 
    } 
} 

XHTML文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:head> 
    <title>Simple JSF Facelets page</title> 
</h:head> 

<h:body> 
    <ui:composition template="layout.xhtml"> 
     <ui:define name="content"> 
      <h:link value="First page" outcome="index.xhtml"></h:link> 
      <br/> 
      <h:link value="Third page" outcome="third.xhtml"></h:link> 
      <br/> 
      Hello #{usersBean.login} with pass #{usersBean.password} 
      <br/> 
      You are #{usersBean.checkRegistred()} 
     </ui:define> 
    </ui:composition> 
</h:body> 

</html> 

回答

0

看看到this question及其答案。在你的情況,因爲你正在使用XML配置,對於@Autowired註解工作,此外,您還需要修改userServide declaratin如下:

<bean id="userService" class="service.UserServiceImpl" autowire-candidate="true"> 
    <property name="userDao" ref="userDao"/> 
</bean> 

隨着autowire-candidate屬性設置爲true,userService將可自動裝配。

更新時間:

package beans; 

import ... 

@SessionScoped 
public class UsersBean implements Serializable { 

    private String login; 

    private String password; 

    @Autowired 
    private UserService userService; 

    ...Getters, setters for every field... 

    public void checkRegistred(){ 
     userService.checkUser(getLogin()); 
    } 

} 

而且,在你的XML應該發現:

<context:component-scan base-package="beans.*" /> 
<context:annotation-config /> 
+0

現在我有另一個錯誤:javax.el.PropertyNotFoundException:/index.xhtml @ 23114值= 「#{usersBean.login}」:目標無法訪問,標識符'usersBean'解析爲空 它在線: sss

+0

你defi按照我上面描述的方式定製'UsersBean'?它看起來錯誤和以前一樣:usersBean爲空。 – jddsantaella

+0

這是因爲faces-config.xml不在WEB-INF目錄下。愚蠢的錯誤。感謝幫助。 – sss