Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to render a component based on condition

 
srikanth Thandra
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi all,
I have a component say,<h:selectOneMenu> .To this i am binding values using <f:selectItems>. In the backing bean i am getting values from database into a map and i am binding that map to <f:selectItems> like as shown below:

<h:selectOneMenu>
<f:selectItems value="#{sessionScope.mapValue}"/>
</h:selectOneMenu>

Now my requirement is that when mapValue is in sessionScope then only <h:selectOneMenu> should render. How can we achieve this in jsf.



Someone throw some light on this. ..
 
Rhys Emmerson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I think something like this might work.



Most other components will have a rendered attribute, it's strange that selectItems doesn't since it's just rendering <option> tags.
 
srikanth Thandra
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But when value binded to f:selectItems is null the page is not loading in the browser.

In my page i have a <h:selectOneMenu> coponent with <f:selectItems>. To this selecItems Iam binding a sessionScope map like

<f:selectItems value="#{sessionScope.valuesMap}"/>

If i write like below in my page
<h:selectOneMenu>
<f:selectItems value="#{sessionScope.valuesMap}"/>
</h:selectOneMenu>

In the browser i am getting error like selectItems found null. and the page is not displaying. How can we write the null checking condition on this so that the page can render normally.
 
Rhys Emmerson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth Thandra wrote:
But when value binded to f:selectItems is null the page is not loading in the browser.

In my page i have a <h:selectOneMenu> coponent with <f:selectItems>. To this selecItems Iam binding a sessionScope map like

<f:selectItems value="#{sessionScope.valuesMap}"/>

If i write like below in my page
<h:selectOneMenu>
<f:selectItems value="#{sessionScope.valuesMap}"/>
</h:selectOneMenu>

In the browser i am getting error like selectItems found null. and the page is not displaying. How can we write the null checking condition on this so that the page can render normally.


That isn't what you asked. I tried this with JSF 2.0 and 1.2 and couldn't replicate the error, when selectitems gets null it just doesn't render. It's most likely an exception that is being propagated up to the selectItems component or it might be something like if a map key/value is null, either way the solution is in your Java code rather than the presentation code.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic