The reasons to use binding in a JSF form

不打扰是莪最后的温柔 提交于 2019-12-11 07:39:57

问题


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}" /> 

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


回答1:


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);
}

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.

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

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.

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?



回答2:


The <h:form> tag can be bound to a backing bean's property that has the same type of the tag HTMLForm - just like the other usual tags.

See also: Difference between value and binding



来源:https://stackoverflow.com/questions/18955836/the-reasons-to-use-binding-in-a-jsf-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!