简体   繁体   English

使用JSF2 + Ajax调用方法

[英]Calling a method using JSF2 + ajax

I've seen a lot of examples online of code where a method is called with a "f:ajax" tag but the name of the method gets shortened in the tag when the method name starts with "get". 我在网上看到了很多示例代码,其中使用“ f:ajax”标记调用方法,但是当方法名称以“ get”开头时,标记中的方法名称会缩短。 I haven't been able to find the reason for this. 我还没有找到原因。 Below is an example of what I mean. 以下是我的意思的示例。

For instance, in the xhtml file "sayWelcome" is called: 例如,在xhtml文件中,“ sayWelcome”被称为:

...
<h:commandButton value="Welcome Me">
    <f:ajax execute="name" render="output" />
</h:commandButton>
<h:outputText id="output" value="#{helloBean.sayWelcome}" />
...

but the method in the bean is called "getSayWelcome": 但是bean中的方法称为“ getSayWelcome”:

public String getSayWelcome(){
       return  name;
}

Why does the "get" get dropped from the method name in the "f:ajax" tag? 为什么“ get”会从“ f:ajax”标签中的方法名称中删除?

The JavaBeansSpecification defines the naming convention for the properties adding the get and set before the functions that acts as properties, see this article for more info. JavaBeansSpecification为属性定义了命名约定,在充当属性的函数之前添加了get和set,有关更多信息,请参见本文

The expression language that use JSF also complains the JavaBeans properties name convention you can check this in this article , refer to the Referring to Object Properties Using Value Expressions section. 使用JSF的表达式语言还抱怨JavaBeans属性名称约定,您可以在本文中进行检查,请参阅“使用值表达式引用对象属性”部分。

So in conclusion when the EL (Expression language) finds a sentence as <h:outputText id="output" value="#{helloBean.sayWelcome}" /> it will try to call a getter or the setter function for the sayWelcome property, it depends if it need to assign the value (setter) or get the value (getter). 因此,总之,当EL(表达式语言)找到一个句子为<h:outputText id="output" value="#{helloBean.sayWelcome}" /> ,它将尝试为sayWelcome属性调用getter或setter函数,这取决于是否需要分配值(setter)或获取值(getter)。

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

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