0

Im am experimenting a bit with JSF, the scenario is the following:

I have a session scoped managed bean

@ManagedBean(name = "rand")
@SessionScoped

where i have declared the following (among other things):

private UIOutput uiOutput;(plus getter and setter)

In my facelets page i have

<h:outputLabel for="userGuess"  binding="#{rand.uiOutput}" 
value="#{rand.listSize}" rendered="false"/>

there is a button on the page, and on the action method of the button among other things i have

this.uiOutput.setRendered(true);

But it does not seem to work, the element will not be rendered.

If a start it as rendered and change the attribute (setting it true or false) it works. I suspect it has something to do with the fact that the element starting as not rendered is not bound to the uiOutput object i have in my managed bean. How can i make it work in this case?

B11
  • 223
  • 3
  • 12
  • 2
    It would be better to bind to a bean property: `rendered="#{rand.someBoolean}"` – McDowell Jan 09 '13 at 10:36
  • i totally agree, and have tested that and it works, i am just experimenting with binding to a component rather than to a value. – B11 Jan 09 '13 at 10:38
  • okie no doubt question is well explained.but I want to know that you want to check "how can we render using managed bean" or "functionality of UIOutput" ? – Freak Jan 09 '13 at 10:38

2 Answers2

1

There is one thing i changed and now it is working, i changed the return type of the method i run when pressing the button, it returned a string value, navigating to the same page, it now returns null and JSF does not generate a new view , instead it renders the same view, and all is working.

I guess since a new view was rendered each time the fact that i set the render value true or false did not matter since each time the component was set to rendered="false" on the new view.

B11
  • 223
  • 3
  • 12
  • 1
    It works now because your command button triggers a full page refresh and the new value of `rendered` is taken into account when building the DOM tree – kolossus Jan 09 '13 at 14:05
  • in the java ee tutorial i see this: After setting the components’ rendered properties to true, this method returns the logical outcome null. This causes the JavaServer Faces implementation to rerender the page without creating a new view of the page, retaining the customer’s input. So my understanding is that it keeps the same object, and simply updates that setting. Is this correct? – B11 Jan 09 '13 at 14:39
  • Yes but probably not the way you think. It won't create a new view in that the viewId remains in the user's session, **but**, the entire DOM tree (markup and all) is rebuilt using updated instructions from the server side – kolossus Jan 09 '13 at 15:01
  • yes, i understand that there is the recreation of all the DOM elements involved, maybe i made a bit of confusion with the life cycle of the backing bean, which depends on it being a session or view or request scoped bean thanks – B11 Jan 09 '13 at 15:12
0

If you are making an ajax call with the button then javascript can not find your element in the dom tree to render it because it is not there in the firstplace.

There is a similar question here which explains the situation better.

Render component with ajax in jsf

Community
  • 1
  • 1
cubbuk
  • 7,800
  • 4
  • 35
  • 62