• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Evaluate 2 expressions using JSTL if tag

Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have an application using JSF with JSTL.
My requirement is as follows:

In my page, I have a user id by which the user has logged in and a list of users who are available.

Now, when I log in, I need to display all users� information. However, the logged in user information needs to be shown as a hyperlink.

For this, I have used the JSTL if tag as follows:

<c:if test="${session.userBean.userName == trip.createdBy}">
<h:commandLink action="#{trip.getDetails}" immediate="true">
<h utputText value="#{tripto.tripId}" style="text-align:center" />
<f aram name="tripId" value="#{tripto.tripId}"/>
<f aram name="tripType" value="#{tripto.tripType}"/>

<c:if test="${ session.userBean.userName != trip.createdBy }">
<h utputText value="#{tripto.tripId}" style="text-align:center" />

Here, userBean and trip are java beans used in the page.
Trip.getDetails is the method in the bean.

But to my surprise even the user id is same as createdBy, the control always goes in the second if condition.

Can anyone let me know what could be the problem in this code?

I tried adding a method in the trip bean called compareValues which takes user id and createdBy and returns a Boolean value after comparing them if they are same.

To call this method form the page I tried adding the following code:

<c:if test={trip. compareValues(session. userBean.userName, trip.createdBy}>

But I get an error �Cannot call method without prefix� when I try to run the page.

How do we call a method in the <c:if> tag?

In short, how to use the c:if tag as per my requirement?

Thanks In Advance
Posts: 67266
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do we call a method in the <c:if> tag?

You don't. The EL is not a general-purpose programming language; you cannot use it to call general methods. In JSP 2.0 you can define functions, but they are limited to calling static methods and cannot be used to call instance methods of beans.

In short, how to use the c:if tag as per my requirement?

In the following example:

you appear to have coded it correctly. If the evaluated values of session.userBean.userName and trip.createdBy are equal, the content of the <c:if> tag will be evaluated. If it is not being evaluated, then the values are not equal. If you think that they are, you need to check them very carefully; they cannot be.

By the way, using two <c:if> statements, with opposite expressions, to represent an if-then-else pattern isn't a great idea. It is possible for the two expressions to get out of synch with each other too easily over the course of additions and maintenance to the page.

A more common pattern for representing an if-then-else would be along the lines of:

Note that in this idiom, the expression is coded once so the chance of introducing errors if the expression needs to be changed is eliminated.

Also, please check the 'disable smilies' checkbox when posting code.
[ May 12, 2005: Message edited by: Bear Bibeault ]
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Thanks for your reply! Thanks for the recommendation of <c:choose> and <c:when> tag. My problem is at first my expression

<c:when test="${session.userBean.userName == trip.createdBy}">

Is not getting evaluated properly. If it is, then atlest for time it has to go in

<c:when test="${session.userBean.userName == trip.createdBy}">

and rest of the times in

<c:when test="${session.userBean.userName != trip.createdBy}">

For timebeing it is not my concern to use 2 sapertae <c:if> tags or to use single <c:choose> tag. My focus in on succesfully evaluation of this expression.

After reading more on the google I changed my expression as follows

<c:when test="${session.userBean.userName != requestScope.trip.createdBy}">

To consider the scope of TripTO object, which is referred as 'trip' in this expression.

After all this also, it is not working. Please guide me how to proceed.

Thanks in advance!

P.S. Thanks for this 'Disable similies' option
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!