Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Backing Bean question

 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm newbie to JSF and i am confused on this portion written by Cay Horstmann in the Core Java server faces:

=======================================================================================================

The code:

public class QuizFormBean {
private UIOutput scoreComponent;
private UIInput answerComponent;
// PROPERTY: scoreComponent
public UIOutput getScoreComponent() { return scoreComponent; }
public void setScoreComponent(UIOutput newValue) { scoreComponent = newValue; }

// PROPERTY: answerComponent
public UIInput getAnswerComponent() { return answerComponent; }
public void setAnswerComponent(UIInput newValue) { answerComponent = newValue; }
...
}

=========================================================================================================

This is the 2nd edition of this book.

When you use a backing bean, you need to wire up the components on the form to those on the bean. You use the binding attribute for this purpose:

<h : outputText binding="#{quizForm.scoreComponent}"/>

When the component tree for the form is built, the getScoreComponent method of the backing bean is called, but it returns null. As a result, an output component is constructed and installed into the backing bean with a call to setScoreComponent.

Question:
if this is correct .. why another output component is created? I dont see the need since a backing bean is both ways - input and output.


=========================================================================================================

This is the 3rd edition of this book.

When you use a backing bean, you need to wire up the components on the form to those on the bean. You use the binding attribute for this purpose:

<h : InputText binding="#{quizForm.answerComponent}"/>

When the component tree for the form is built, the getAnswerComponent method of the backing bean is called, but it returns null. As a result, an output component is constructed and installed into the backing bean with a call to setAnswerComponent .

Question:
If this edition is correct, shouldn't this be input component since answerComponent is of type UIInput?

=========================================================================================================
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18008
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a rough guess, I'd suspect that the reason an output component was constructed is that an input component would be useless.

An input component is supposed to be able to set a backing bean property, but if you constructed one without any knowledge of what was going in, you'd have no place to put received input data to begin with. So it would be output-only purely because it had no other abilities.

However, direct control binding is overused by JSF programmers anyway. You should really only use it if you intend to make the backing bean dynamically modify the View tree. For most purposes, the "value=" attribute bound to a data property is quite sufficient.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic