2013-07-01 107 views
1

我創建了一個簡單的應用程序,它使用JSF和託管的bean。來自託管bean我嘗試實例化一個將數據存儲到數據庫的spring bean。然而 「@Autowired」 註解似乎沒有工作,因爲我得到一個nullPointerExcpetion在託管bean中注入彈簧:

javax.faces.el.E​​valuationException:顯示java.lang.NullPointerException

問題出現時調用authenticationComponent。認證()方法

RMQ:我測試ClientService的方法和一切都很好

管理的bean:

@ManagedBean(name="authenticationComponent") 
@RequestScoped 
public class AuthenticationComponent implements Serializable { 

    private static final long serialVersionUID = -5085727127290746426L; 
    private static final String SUCCESS = "success"; 
    private static final String ERROR = "error"; 

    @Autowired 
    ClientService clientService; 
    Client client; 
    String clientName; 
    String clientPass; 
    public String authenticate() { 

     client = clientService.authenticate(clientName, clientPass); 
     if (client != null) { 
      return SUCCESS; 
     } 
     return ERROR; 

    } 

authentification.xhtml

   <p:inputText id="userName" required="true" 
        label="User Name" title="Enter Your Name !" value="#{authenticationComponent.clientName}"/> 
       <p:tooltip for="userName" styleClass="tooltip" showEvent="focus" 
        hideEvent="blur" /> 

       <h:panelGroup> 
        <h:outputText value="User Password : " /> 
        <h:outputText style="color:red" value="* " /> 
       </h:panelGroup> 

       <p:inputText id="userPass" required="true" 
        label="User Password" title="Enter Your Password !" value="#{authenticationComponent.clientPass}" /> 
        <!-- 
          label option is needed for messages component 
        --> 
       <p:tooltip for="userPass" styleClass="tooltip" showEvent="focus" 
        hideEvent="blur" /> 

       <p:commandButton id="submitLoginButton" 
        update="loginPanelGrid,messages" value="Sign In" 
        icon="ui-icon-check" action="#{authenticationComponent.authenticate()}" /> 
       <p:commandButton id="newUserButton" 
        update="loginPanelGrid,messages" value="Sign Up" 
        icon="ui-icon-plus" /> 

的applicationContext:

<bean id="clientService" class="com.webapp.service.ClientServiceImpl"> 
      <property name="clientDao" ref="clientDao" /> 
     </bean> 
<bean id="genericDao" class="com.webapp.dao.GenericDaoImpl" abstract="true" > 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="clientDao" class="com.webapp.dao.ClientDaoImpl" parent="genericDao"> 
     <constructor-arg ref="client" /> 
    </bean> 

    <bean id="client" class="com.webapp.model.Client"/> 
+0

您正在將JSF託管的bean與spring beans混淆。 Spring將不會檢測@Autowired,因爲該bean是由JSF框架創建的應用程序上下文之外的。 – ElderMael

+0

謝謝,請使用什麼然後注入ClientService? –

回答

1

首先,你需要一個定製的EL-解析器在faces-config.xml文件:

<application> 
     <!-- For DI of beans from Spring WebApplicationContext --> 
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application> 

然後,你將需要使用註釋@ManagedProperty這樣的:

@ManagedProperty(value = "#{clientService}") 
ClientService clientService; 

所以春季EL-解析器可以通過名稱注入您的服務bean。

+0

我有這個錯誤:錯誤渲染視圖[/ welcome.xhtml] com.sun.faces.mgbean.ManagedBeanCreationException:無法創建託管的「authenticationComponent」bean。檢測到以下問題: - 託管bean「authenticationComponent」的「ClientService」屬性不存在。 –

+0

您是否創建了clientService getter和setter?然後測試。 – ElderMael

+0

它的工作!謝謝 –