1

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.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
Atul Rane
  • 11
  • 1
  • 3
  • possible duplicate of [Difference between value and binding](http://stackoverflow.com/questions/13681697/difference-between-value-and-binding) – sleske Sep 23 '13 at 10:48
  • possible duplicate of [What is Component Binding in JSF ? When it is preferred to be used?](http://stackoverflow.com/questions/12506679/what-is-component-binding-in-jsf-when-it-is-preferred-to-be-used) – BalusC Sep 23 '13 at 10:59
  • And then on related note: [how binding attribute in JSF works](http://stackoverflow.com/questions/14911158/how-binding-attribute-in-jsf-works) I'd say, it's poor design in this form. – BalusC Sep 23 '13 at 11:00

2 Answers2

2

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:

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • thank you very much for your answer. its really helpful for me. I have another doubt, I am doing analysis of legacy code which is build using JSF. I am facing problem to indetify how the values are being populated to the plain Beans (pojo) from the XHTML file when we submit the form. – Atul Rane Oct 10 '13 at 09:57
  • You're welcome! In case you have another question click on a 'Ask question' button and post it. Also, as you're new here be sure to accept the answer that (most) helped you in solving your problem. – skuntsel Oct 10 '13 at 10:18
1

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

Community
  • 1
  • 1
Omar
  • 1,430
  • 1
  • 14
  • 31