Spring-JSF2集成:目标无法访问,标识符'customerBean'解析为空
问题描述:
我正在尝试使用注释(不使用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>
有谁知道我在做什么错在这里?
谢谢!
答
我只是改变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>