Spring-JSF2集成:目标无法访问,标识符'customerBean'已解析为null

时间:2011-09-24 22:38:56

标签: java spring jsf-2 annotations

我正在尝试仅使用注释对Spring 3和JSF 2进行简单集成(不使用faces-config.xml来定义托管bean),而且我遇到了错误。

错误是:

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>

这是bean: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>

有谁知道我在这里做错了什么?

谢谢!

1 个答案:

答案 0 :(得分: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>