Conditionally displaying JSF components
Asked Answered
S

3

86

First, I am new to Java EE, came from a strong ASP .NET development background. I have gone through the net, and I might miss this but it seems like there is no simple and straight-to-the-point tutorials on how I could connect backing bean class to a JSF components.

A good example is like this, currently I am trying to create a JSF page where there is a set of links as menu bar and a set of forms. What I am planning to do is, when clicking a link, a particular form will be rendered.

In ASP.NET, I could easily retrieve the element and then set the attribute to be displayable. I am wondering if there is easy way (heck, even any way) to do this in JSF.

The forms is already in the page, it is just a matter of setting the "render" attribute to true when I click a particular link.

Sociology answered 2/2, 2011 at 3:17 Comment(0)
S
175

Yes, use the rendered attribute.

<h:form rendered="#{some boolean condition}">

You usually tie it to the model rather than letting the model grab the component and manipulate it.

E.g.

<h:form rendered="#{bean.booleanValue}" />
<h:form rendered="#{bean.intValue gt 10}" />
<h:form rendered="#{bean.objectValue eq null}" />
<h:form rendered="#{bean.stringValue ne 'someValue'}" />
<h:form rendered="#{not empty bean.collectionValue}" />
<h:form rendered="#{not bean.booleanValue and bean.intValue ne 0}" />
<h:form rendered="#{bean.enumValue eq 'ONE' or bean.enumValue eq 'TWO'}" />

Note the importance of keyword based EL operators such as gt, ge, le and lt instead of >, >=, <= and < as angle brackets < and > are reserved characters in XML. See also this related Q&A: Error parsing XHTML: The content of elements must consist of well-formed character data or markup.

As to your specific use case, let's assume that the link is passing a parameter like below:

<a href="page.xhtml?form=1">link</a>

You can then show the form as below:

<h:form rendered="#{param.form eq '1'}">

(the #{param} is an implicit EL object referring to a Map representing the request parameters)

See also:

Soren answered 2/2, 2011 at 3:41 Comment(0)
C
15

In addition to previous post you can have

<h:form rendered="#{!bean.boolvalue}" />
<h:form rendered="#{bean.textvalue == 'value'}" />

Jsf 2.0

Cindelyn answered 11/10, 2012 at 21:24 Comment(1)
Such a small addition is not worth a separate answer.Amarelle
M
0

we can render a component in the form, for that we will use 'rendered' attribute

This attribute takes true/false. If the expression evaluates to a true then the specified component will be rendered otherwise it wont render in the screen.

Ex: I want to render a button for some condition.

<p:commandButton id="addCommentsBtn"
    value="Add Comments" title="Add Comments"
    rendered="#{backingBean.booleanResultOrVariable}">
</p:commandButton>
Maddox answered 24/3, 2021 at 17:9 Comment(3)
This is correct, but the question was about rendering, not disabling. Showing information that the user is not allowed to see doesn't get better if you just disable it.Auld
Thanks Robert, I have modified my solutionMaddox
Stack Overflow is a Q&A site, not a discussion forum. It's on a Q&A site not necessary to repeat an already given answer upon agreement. Just delete it.Soren

© 2022 - 2024 — McMap. All rights reserved.