0

I have a form to select exactly one agent from a list. The backing bean decides if the list should be rendered and fills the items of the radio buttons:

<h:selectOneRadio rendered="#{myBean.shoudRender}" value="#{myBean.selectedAgent}" id="agents">
    <f:selectItems value="#{myBean.allAgents}" />
</h:selectOneRadio>

Is it 100% sure, that myBean.getShouldRender() will execute before myBean.getAllAgents?

Thanks!

user1414745
  • 1,317
  • 6
  • 25
  • 45

2 Answers2

3

Yes, it will. The UIComponent#encodeAll() will check if isRendered() returns true before continuing with encoding itself and its children.

On the other hand, this suggests that you're performing business logic in the getter of the <f:selectItems>. Otherwise, you wouldn't worry about it at all for the case it returns null or so and never have asked this question in first place. The getter method is the wrong place for performing business logic. You should do that in the (post)constructor or (action)listener method instead. The getter should solely return the already-prepared value.

Thus, this is wrong:

public boolean isShouldRender() {
    boolean shouldRender = // Some business logic...
    // ...

    return shouldRender;
}

public List<Agent> getAllAgents() {
    List<Agent> allAgents = // Some business logic...
    // ...

    return allAgents ;
}

Instead, you should do

// Have properties which you initialize during an event.
private boolean shouldRender;
private List<Agent> allAgents;

public void someEventMethod() { // E.g. postconstruct, action, ajax behavior, value change, etc.
    shouldRender = // Some business logic.
    allAgents = // Some business logic.
}

// Keep the getters untouched!
public boolean isShouldRender() {
    return shouldRender;
}

public List<Agent> getAllAgents() {
    return allAgents;
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Great answer, thanks! I do have business logic in the getter methods - there is an expensive service call in both `myBean.isShouldRender()` and `myBean.getAllAgent`. I wanted to call the service only in `myBean.isShouldRender()`. But you are right, I should call the service earlier. The best option for me is to call the service before the page is rendered. I found this post: [JSF 2 invoke action method on page load](http://itakeitalltheway.blogspot.de/2010/09/jsf-2-invoke-action-method-on-page-load.html) – user1414745 Apr 05 '13 at 07:23
  • I think the best option will be: `` `` ` ` – user1414745 Apr 05 '13 at 07:26
  • Or just the bean's (post)constructor. See also [preRenderView versus PostConstruct](http://stackoverflow.com/questions/9844526/when-to-use-prerenderview-versus-postconstruct/9844616#9844616) – BalusC Apr 05 '13 at 10:56
0

I work in a technology called XPages with is based based off of JSF.

In my world at least rendered would be evaluated first. So I think you will be ok.

David Leedy
  • 3,583
  • 18
  • 38