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

发布于 2024-12-06 08:28:08 字数 2618 浏览 0 评论 0原文

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

错误是:

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>

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

谢谢!

I'm trying a simple integration of Spring 3 and JSF 2 using annotations only (not using faces-config.xml to define managed beans) and I'm stuck with an error.

The error is:

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

This is the page: 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>

This is the 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
}

This is a part of my applicationContext.xml

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

This is a part of my 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>

Does anyone know what I'm doing wrong here?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-12-13 08:28:09

我刚刚将 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>

I just changed CustomerBean.java to have the following annotations:

@Component
@Scope("session")

Then added SpringBeanFacesELResolver in faces-config.xml. I had it commented out before, not knowing that I needed it even if I won't use faces-config to define 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文