2011-09-24 34 views
2

我正在嘗試使用註釋(不使用faces-config.xml來定義託管bean)的Spring 3和JSF 2的簡單集成,米卡住了一個錯誤。Spring-JSF2集成:目標無法訪問,標識符'customerBean'解析爲空

的錯誤是:

javax.el.PropertyNotFoundException: /customer/add.xhtml @11,70 value="#{customerBean.firstName}": Target Unreachable, identifier 'customerBean' resolved to null 

這是網頁:add.xhtml

<h:body> 
    <h:form> 
     <label>First Name <h:inputText value="#{customerBean.firstName}" /></label><br /> 
     <label>Last Name <h:inputText value="#{customerBean.lastName}" /></label><br /> 
     <label>Email <h:inputText value="#{customerBean.email}" /></label><br /> 
     <h:commandButton value="Add" action="#{customerBean.add}" /> 
    </h:form> 
</h:body> 

這是豆:CustomerBean.java

package com.devworkzph.customer.sample.bean; 

@Component 
@Qualifier("customerBean") 
@SessionScoped 
public class CustomerBean implements Serializable{ 
    private String firstName; 
    private String lastName; 
    private String email; 

    public String add(){ 
     // code 
    } 
    //getters and setters 
} 

這是一個我的一部分applicationContext.xml

<context:annotation-config /> 
<context:component-scan base-package="com.devworkzph.customer.sample" /> 

這是我的web.xml的一部分

<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>*.xhtml</url-pattern> 
</servlet-mapping> 
<context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
</context-param> 

有誰知道我在做什麼錯在這裏?

謝謝!

回答

0

我只是改變CustomerBean.java有以下注釋:

@Component 
@Scope("session") 

然後在faces-config.xml中添加SpringBeanFacesELResolver。之前我已經註釋過了,即使我不使用faces-config來定義CustomerBean,也不知道我需要它。

<?xml version="1.0"?> 
<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> 
    </application> 
</faces-config> 
相關問題