简体   繁体   English

JSF AJAX-类没有属性

[英]JSF AJAX - Class doesn't have property

I have been trying to run AJAX example in JSF. 我一直试图在JSF中运行AJAX示例。 But I am getting "class does not have the property login". 但是我得到“类没有属性登录名”。 But in all the examples in various websites, code is same the same. 但是在各个网站的所有示例中,代码都是相同的。

My index.xhtml 我的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 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;
    }
}

This is my error, 这是我的错误

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

How do I resolve this error ? 如何解决此错误?

Value and Method Expressions 值和方法表达式

The EL defines two kinds of expressions: value expressions and method expressions. EL定义了两种表达式:值表达式和方法表达式。 Value expressions can either yield a value or set a value. 值表达式可以产生一个值或设置一个值。 Method expressions reference methods that can be invoked and can return a value. 方法表达式引用可以调用并可以返回值的方法。

Example of a value expression according to the above definition in your code is: 根据代码中上述定义的值表达式的示例是:

userData.name

In the following tag definition: 在以下标记定义中:

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

you are not using a value expression but rather a method expression because login is not a simple JavaBean getter returning the value of a bean property. 您不是在使用值表达式而是方法表达式,因为login不是返回bean属性值的简单JavaBean getter。

So you have to change the above line as: 因此,您必须将上面的行更改为:

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

And remove your login or use it for navigation purpose (that is why it is there). 并删除您的登录名或将其用于导航目的(这就是为什么存在该登录名的原因)。

Here is the reason (taken from JSF 2.0 specification) why you have to pass a value expression to an output component instead of a method expression: 这是原因(取自JSF 2.0规范),为什么必须将值表达式传递给输出组件而不是方法表达式:

4.1.10 UIOutput 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. UIOutput(扩展UIComponentBase;实现ValueHolder)是一个具有值的组件,可以选择通过值表达式 (请参见第5.1节“值表达式”)从模型层bean中检索该 ,并显示给用户。 The user cannot directly modify the rendered value; 用户不能直接修改渲染的值。 it is for display purposes only: 它仅用于显示目的:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM