以JSF形式使用绑定的原因 [英] The reasons to use binding in a JSF form

查看:111
本文介绍了以JSF形式使用绑定的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSF的新手.谁能解释一下为什么在下面的代码中使用binding属性吗?

I am new to the JSF. Can anybody please explain me why binding attribute is used in the code below:

<h:form id="epox" binding="#{rxManufacturerEditor.form}" /> 

我对valuebinding属性有点困惑,但是我不明白为什么我们要提到带有表单标签的绑定属性.

I am a bit confused with value and binding attributes, however I am not getting why we mention binding attribute with form tag.

推荐答案

使用绑定到支持bean的UIComponent实例的唯一原因(我知道)是能够以编程方式操作该组件在action/actionlistener方法或ajax侦听器方法中,例如:

The only reason to use binding to a backing bean's UIComponent instance that I know of is the ability to manipulate that component programmatically within an action/actionlistener method, or ajax listener method, like in:

UIInput programmaticInput;//getter+setter
String value1, value2;//getter+setter
...
public void modifyInput() {
    ELContext ctx = FacesContext.getCurrentInstance().getELContext();
    ValueExpression ve = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(ctx, "#{bean.value2}", Object.class);
    programmaticInput.setValueExpression("value", ve);
}

触发操作方法后,组件<h:inputText value="#{bean.value1}" binding="#{bean.programmaticInput} ... />的值将绑定到value2而不是value1.

After the action method has been triggered the value of component <h:inputText value="#{bean.value1}" binding="#{bean.programmaticInput} ... /> will be bound to value2 instead of value1.

我很少使用这种类型的绑定,因为facelets提供了基于XML的视图定义,而不必(定期)弄乱程序化组件.

I rarely use this type of binding, because facelets offer an XML-based view definition without the necessity to (regularly) mess with programmatic components.

确保知道上述构造在2.1.18之前的Mojarra版本中失败,从而强制在每个HTTP请求上重新创建视图范围的Bean.有关更多详细信息,请参见 @ViewScoped在标记处理程序中失败.

Be sure to know that the abovementioned construct fails in Mojarra version older than 2.1.18, forcing view scoped beans to be recreated on every HTTP request. For more details refer to @ViewScoped fails in tag handlers.

通常,您希望使用绑定到视图,在其中可以进行跨字段验证:

More typically, you'd want to use binding to the view in which you can do cross-field validation:

<h:inputText binding="#{input}" ... />
<h:inputText validator="#{bean.validate}" ... >
    <f:attribute name="input" value="#{input}" />
</h:inputText>

在这里,整个第一个输入组件将作为第二个组件的属性可用,因此其值将在关联的验证器(方法)中可用.另一个示例是检查视图中已触发了哪些命令组件:

Here, the whole first input component will be available as an attribute of the second component and therefore its value will be available in the associated validator (method). Another example is to check which of the command components has been triggered in view:

<h:commandButton binding="#{button}" ... />
<h:inputText disabled="#{not empty param[button.clientId]}" ... />

在这里,仅当按下按钮时,输入文本组件才会被禁用.

Here, the input text component will be disabled only when the button was pressed.

有关更多信息,请继续阅读BalusC的以下答案:

For more information proceed to the follwing answers by BalusC:

  • What is component binding in JSF? When it is preferred to be used?
  • How does the 'binding' attribute work in JSF? When and how should it be used?

这篇关于以JSF形式使用绑定的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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