EL#{bean.id}如何调用托管bean方法bean.getId() [英] How does EL #{bean.id} call managed bean method bean.getId()

查看:135
本文介绍了EL#{bean.id}如何调用托管bean方法bean.getId()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我并不真正理解吸气剂和吸气剂的工作原理,尽管这是一个基本概念.我有以下代码,属性id如何发送到Managed Bean?是通过吸气剂方法捕获的吗?

I do not really understand how getter and setter work althougth it is a basic concept. I have the following code, how is the attribute id sent to Managed Bean? Is it captured by getter method?

我的面孔

<p:inputText id="id" value="#{bean.id}">

我的托管bean

private String id;

public void setId(String id) {
    this.id = id;
}

public String getId() {
      return id;
}

推荐答案

#{}表达式对getter和setter方法的调用不是JSF的一部分,而是

The call of getter and setter methods by #{} expressions is not part of JSF but Expression Language (most known as EL). JSF takes advantage of EL to bind the data of the HTML components to the fields of a bean through proper getters and setters. This is:

  • 如果该bean存在,则表达式语言将在适当的范围内执行已注册bean的适当getter.
  • 如果客户端执行表单提交或ajax请求,则发送到服务器的组件(通常是<h:form>中的所有组件,如果有ajax请求,则可以声明要发送到服务器的组件)将包含一个新值,并将使用适当的setter方法将该值设置为该字段.
  • If the bean exists, Expression Language will execute the proper getter of the registered bean in the proper scope.
  • If client performs a form submission or an ajax request, then the components that are sent to the server (usually all the components in the <h:form>, in case of ajax requests you can state which components to send to the server) will contain a new value, and this value will be set to the field with the proper setter method.

例如,您有一个属于请求范围的SayHelloBean:

For example, you have a SayHelloBean which belongs to request scope:

@RequestScoped
@ManagedBean
public class LoginBean {
    private String name;
    //proper getter
    public String getName() {
        return this.name;
    }
    //proper setter
    public void setName(String name) {
        this.name = name;
    }
}

这两个facelets页面(因为这是一个示例,因此我避免声明<html><h:head><h:body>和其他元素,而只关注相关代码)

And these 2 facelets pages (since it's an example I avoid declaring <html>, <h:head>, <h:body> and other elements, just focusing on the relevant code)

Page1.xhtml:

Page1.xhtml:

<h:form>
    Please tell me your name
    <h:inputText value="#{loginBean.name}" />
    <h:commandButton action="page2" />
</h:form>

Page2.xhtml:

Page2.xhtml:

Hello #{loginBean.name}

这是幕后发生的事情:

  1. 当加载Page1.xhtml时,JSF将创建一个新的LoginBean实例(我们称为loginBean)并将其注册到JSP请求范围中.由于<h:inputText />的值绑定到LoginBean#name(将其读取为 LoginBean类的字段name 的字段),因此EL将显示loginBean#name的值(即读为实例loginBean 的字段name),由于未初始化,因此EL将显示null为空字符串.

  1. When Page1.xhtml is loaded, a new instance of LoginBean, which we may call loginBean, will be created by JSF and registered into JSP request scope. Since the value of <h:inputText /> is bound to LoginBean#name (which is read as the field name of LoginBean class), then EL will display the value of loginBean#name (which is read as the field name of instance loginBean), and since that is not initialized, EL will display null, as an empty string.

当您提交Page1.xhtml的格式时,由于LoginBean@RequestScoped,因此JSF将创建一个LoginBean的新实例,我们可以将其称为loginBean2(在其中添加2最后,因为此实例与之前创建的loginBean完全不同),并将其注册到JSP请求范围中.由于<h:inputText />的值绑定到LoginBean#name,因此JSF将通过调用适当的setter来验证和设置数据.这将使loginBean2#name具有由<h:inputText/>呈现的<input type="text">的值.

When you submit the form of Page1.xhtml, since LoginBean is @RequestScoped then JSF will create a new instance of LoginBean, which we may call it loginBean2 (adding 2 in the end because this instance is totally different from the loginBean previously created) and will register it in JSP request scope. Since the value of <h:inputText /> is bound to LoginBean#name, JSF will validate and set the data by calling the proper setter. This will make loginBean2#name have the value of the <input type="text"> that was rendered by <h:inputText/>.

最后,JSF将确保向前导航到Page2.xhtml,在进行处理时,它将找到#{loginBean.name},EL将检查loginBean2#name的值并将其替换.

At last, JSF will make sure to navigate to Page2.xhtml through forward, where when processing it, it will find #{loginBean.name} and EL will check for the value of loginBean2#name and replace it.

这里解释的步骤只是对JSF生命周期的一个很小的解释(很多元素没有解释),以及JSF如何使用getter和setters.

The steps explained here are a very small explanation (and with lot of elements not explained) of the JSF lifecycle and how JSF uses getters and setters.

更多信息:

  • How to pass parameter to jsp:include via c:set? What are the scopes of the variables in JSP?
  • How to choose the right bean scope?
  • The Lifecycle of a JavaServer Faces Application
  • Differences between Forward and Redirect

附加说明:由于您正在学习JSF,因此请避免将任何业务逻辑代码放入getter/setter中.这在这里得到了很大的解释:为什么JSF多次调用getters

Additional note: since you're learning JSF, avoid putting any business logic code in getters/setters. This is greatly explained here: Why JSF calls getters multiple times

这篇关于EL#{bean.id}如何调用托管bean方法bean.getId()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆