Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HOW: 'if'-like JSF conditions?  RSS feed

 
J. Begham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi group,

I'd like to show / hide some parts / sections of my JSF page depending on whether the user is logged in or the user has propper rights.

E.g. if a user is already logged in, the login section should no longer be shown.

in a normal JSP page, I would do this by something like


but I have absolutly no idea how to do this in a JSF way.

I know that most of the jsf-tags have an render-attribute, but in my opinion it is not the right approach, as you would have to set it for each element which should be shown/ should be hidden.

So what I am looking for is something what gives me the possibility to just hide/show everything between one tag like the if-brackets in the "old" way.

How can it be done in JSF?

Thanks in advance
John
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of JSF 1.2 and Servlets 2.5, JSTL 1.2 is supposed to work well with JSF. So give a try to <c:if> and <c:choose> tags and let us know about the results.

Example :

<c:if test="${empty param.userName}">
do things here ...
</c:if>

<c:if test="${initParam.showAdvice == 'yes'}">
do things here ...
</c:if>

<c:choose>
<c:when test="${sessionScope.shoppingCart.itemsCount > 0}">
do things here ...
</c:when>

<c:when test="${applicationScope.databaseName == 'MySQL'}">
do things here ...
</c:when>

<ctherwise>
do other things here ...
</ctherwise>
</c:choose>

and so on ....
[ June 04, 2007: Message edited by: Edisandro Bessa ]
 
J. Begham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

thanks for the help. I think the JSF 1.2 Servlet 2.5 JSTL 1.2 solution is what I am looking for. Unfortunatly the environment basis isn't ready yet for this state-of-the art technology, so it will take some time to get the "good solution" running.

I have found a "workaround" solution with using a myFaces Tomahawk Div extension which I can use as a surrounding element which can hold the rendered-Attribute.

e.g.

In the example above the enclosed content won't be rendered.

Greets
John
 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rendered attribute with something like PanelGrid will do the job.

You don't need to set "rendered" for every other element. only the element you wanna control its hide/show behaviour.

I don't understand why you see it will complicate your design.

I don't think you need JSF1.2 or Tomahawk to acheive your goal. Just group the things you wanna hide/show under one element maybe PanelGrid.

regards,

Majid
 
J. Begham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Majid,

you are right. PanelGroup would do the job as well. I didn't thought of this element.

Greets
John
[ June 04, 2007: Message edited by: J. Begham ]
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSF tags aren't intended to be a programming language. The idea is that all logic "should" be in the backing beans or services referenced by the backing beans, unlike JSTL or Struts. For conditional display of elements, the rendered= attribute works well. That puts the logic in the backing bean, but ensures that propertis of the backing bean will be displayed - or not - in an appropriate manned.
 
Emilio Lb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use html:panelGroup tag to conditionally output an html block

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!