change rendered attribute from managed bean

2020-08-04 12:00发布

问题:

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?

回答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.



回答2:

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