2012-10-14 41 views
3

我正在學習Spring與Hibernate,並創建一個使用JSF作爲前端框架的電影租賃應用程序。NPE當調用託管屬性的獲取者

我在我的註冊bean中有一個應用程序範圍的託管屬性,它是視圖範圍。在用於將用戶詳細信息插入數據庫的register()方法中,我調用服務定位器實現bean以獲取對所需服務實現的引用。但是,當我調用服務定位器屬性的getter時,我得到了一個N​​PE。

以下是我的管理豆...

註冊豆

@ManagedBean 
@ViewScoped 
public class RegistrationBean extends BaseBean implements Serializable 
{ 
    private static final long serialVersionUID = -6449858513581500971L; 

    private String userID; 
    private String password; 
    private String firstName; 
    private String lastName; 
    private String email; 
    private String addressLine1; 
    private String addressLine2; 
    private String city; 
    private String state; 
    private String pincode; 

    public RegistrationBean() { 
     super(); 
    } 

    // getter/setters... 

    public String register() 
    { 
     String nextPage = null; 
     try { 
     RegistrationDetails userDetails = ModelBuilder.populateRegistrationData(this); 

     // NPE at this line. getServiceLocator() returns null 
     int registrationID = getServiceLocator().getUserService().registerUser(userDetails); 

     nextPage = "success"; 
     } 
     catch (RegistrationException e) { 
     LOGGER.error(e.getMessage()); 
     } 
     return nextPage; 
    } 
} 

基地豆

public class BaseBean 
{ 
    @ManagedProperty("#{serviceLocator}") 
    protected IServiceLocator serviceLocator; 

    protected IServiceLocator getServiceLocator() { 
     return serviceLocator; 
    } 

    public void setServiceLocator(IServiceLocator serviceLocator) { 
     this.serviceLocator = serviceLocator; 
    } 
} 

服務定位豆

@ManagedBean 
@ApplicationScoped 
public class ServiceLocator implements IServiceLocator 
{ 
    private static final String USER_SERVICE = "userService"; 

    public ServiceLocator() { 
     super(); 
     final ServletContext sc = FacesUtils.getServletContext(); 
     this.webAppContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); 
     this.userService = (IUserService) webAppContext.getBean(USER_SERVICE); 
    } 

    private ApplicationContext webAppContext; 

    private IUserService userService; 

    @Override 
    public IUserService getUserService() { 
     return userService; 
    } 

    public ApplicationContext getWebAppContext() { 
     return webAppContext; 
    } 
} 

這裏是因爲看到我的Eclipse控制檯堆棧跟蹤

Oct 14, 2012 10:28:39 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute 
WARNING: #{registrationBean.register}: java.lang.NullPointerException 
javax.faces.FacesException: #{registrationBean.register}: java.lang.NullPointerException 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    ... 23 more 
Caused by: java.lang.NullPointerException 
    at com.clixflix.managedbeans.RegistrationBean.register(RegistrationBean.java:118) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
    ... 24 more 

我使用

  • JSF 2.1(鑽嘴魚科)
  • 的Hibernate 4.1
  • 春季3.2
  • Tomcat 7
  • Eclipse 3.7

有人請指出,如果我錯過了什麼?

UPDATE:Spring配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<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="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" /> 
    </bean> 

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="userDAO" class="com.clixflix.dao.impl.UserDAOImpl"> 
     <constructor-arg index="0" ref="hibernateTemplate" /> 
    </bean> 

    <bean id="userService" class="com.clixflix.services.impl.UserService"> 
     <constructor-arg index="0" ref="userDAO" /> 
    </bean> 

</beans> 
+0

@BalusC - 正如我所說的,我_learning_ Spring + Hibernate的,所以我的設計是臨時性的。但不,我的服務定位器不是春天的豆子。它封裝的用戶服務屬性是一個spring bean。我添加了我的spring配置文件。我讀了一篇文章,其中使用Spring MVC使用JSF2.0,但它有點讓人困惑,所以現在我只是使用Spring ORM提供的依賴注入和hibernate模板作爲hibernate部分。 – Vrushank

回答

1

這篇文章幫我做的代碼工作:http://www.javacodegeeks.com/2012/04/jsf-2-primefaces-3-spring-3-hibernate-4.html

但是,代碼不工作,如果我把註冊bean作爲view scoped。任何人都可以請幫我找出相同的原因嗎?

更新:找到了ViewScope問題的解決方案:http://blog.harezmi.com.tr/spring-view-scope-for-jsf-2-users/

+1

這裏的最後一個鏈接被打破了。您可能需要修復該鏈接或添加實際爲您工作的解決方案。否則,這個答案根本不值得閱讀。當您指定一個鏈接時,請在您的問題和/或答案中引用至少一些重要的要點,以便即使鏈接被破壞,它們也能對未來的訪問者有用。 – Lion

+0

@Lion:更新後的鏈接 – Vrushank

+0

@Vrushank:我可以知道爲什麼'get()'方法是同步的嗎?我沒有看到這[這裏](http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/)和[這裏]( http://comdynamics.net/blog/109/spring3-jsf2-view-scope/)。它是強制性的嗎?此外,registerDestructionCallback(),getConversationId()和getViewMap()方法的主體在此處留爲空白。這些方法有一些意義嗎?當這些方法需要按照上一個[鏈接](http://blog.harezmi.com.tr/spring-view-scope-for-jsf-2-users/)所示進行編碼? – Tiny

相關問題