JSF AJAX - Class 没有 属性

JSF AJAX - Class doesn't have property

我一直在尝试 运行 AJAX JSF 中的示例。但是我得到 "class does not have the property login"。但是在各个网站的所有例子中,代码都是一样的。

我的index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>JSF AJAX Calls</title>
    </h:head>
    <h:body>
        <h2>AJAX Example</h2>
        <h:form>
            <h:inputText id="inputName" value="#{userData.name}"></h:inputText>
            <h:commandButton value="Login">
                <f:ajax execute="inputName" render="outputMsg" />
            </h:commandButton>
            <br />
            <hr />
            <h2><h:outputText id="outputMsg" value="#{userData.login}" /></h2>
        </h:form>
    </h:body>
</html>

UserData.java

package com.cyb3rh4wk.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String login()   {
        if ("".equals(name) || name == null)
            return "";
        else
            return "Logged in as " + name;
    }
}

这是我的错误,

/index.xhtml value="#{userData.login}": The class 'com.cyb3rh4wk.test.UserData' does not have the property 'login'.

如何解决此错误?

Value and Method Expressions

The EL defines two kinds of expressions: value expressions and method expressions. Value expressions can either yield a value or set a value. Method expressions reference methods that can be invoked and can return a value.

您的代码中根据上述定义的值表达式示例为:

userData.name

在以下标签定义中:

<h:outputText id="outputMsg" value="#{userData.login}" />

您没有使用值表达式而是方法表达式,因为 login 不是简单的 JavaBean getter 返回 bean 的值 属性 .

因此您必须将上面的行更改为:

<h:outputText id="outputMsg" value="Logged in as : #{userData.name}" />

并删除您的 登录名 或将其用于导航目的(这就是它存在的原因)。

以下是您必须将值表达式而不是方法表达式传递给输出组件的原因(取自 JSF 2.0 规范):

4.1.10 UIOutput

UIOutput (extends UIComponentBase; implements ValueHolder) is a component that has a value, optionally retrieved from a model tier bean via a value expression (see Section 5.1 “Value Expressions”), that is displayed to the user. The user cannot directly modify the rendered value; it is for display purposes only: