my dog learned polymorphism
The moose likes JSF and the fly likes Evaluating 2 expressions in JSF using JSTL if tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Evaluating 2 expressions in JSF using JSTL if tag" Watch "Evaluating 2 expressions in JSF using JSTL if tag" New topic

Evaluating 2 expressions in JSF using JSTL if tag

Dhananjay Inamdar
Ranch Hand

Joined: Jan 27, 2003
Posts: 130

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

Just like you, struggeling to get the right solutions!<br /> <br />Sun Certified Java Programmer 1.5<br /> <br />Target - SCWCD
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I honestly can't tell where your logic is in error. However, I can make a recommendation.

Stop using JSTL. I have to smile when I see people still trying to think of JSP/Servlet programming when dealing with JSF. You really need to shift your mindset. Logic like you are trying to achieve should all be done in Java code.

What you need to do is determine if all that data needs to be shown. Then add a boolean method in your backing bean to support this. Then your JSF code gets real simple:

See how much better that looks?

GenRocket - Experts at Building Test Data
Dhananjay Inamdar
Ranch Hand

Joined: Jan 27, 2003
Posts: 130
Hi Greg,

Thanks for reply and honest recommendation. I tried it out. I have added the following method in

public boolean compareUser(String userName, String createdBy){
return true;
return false;

As my main internsion is to check for the loggin in user is the same of created or not. So I am comparing these 2 strings in this method.

In order to call this method from the JSF page I added following code in my JSF page

<c:if test="${requestScope.trip.compareUser(sessionScope.USER_BEAN.userName, requestScope.tripto.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="${requestScope.trip.compareUser(sessionScope.USER_BEAN.userName, requestScope.tripto.createdBy)}">
<h utputText value="#{requestScope.tripto.createdBy}+ DHan012" />
<h utputText value="#{tripto.tripId}" style="text-align:center" />

Here it is the problem that page is giving me error that I can't call method without prefix.

Will you please guide me how to proceed next?>

Thanks in advance!
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
As Gregg said, avoid using JSTL along with JSF. Things will look cleaner.
JSTL and JSF both write to response stream and it does generates dramatic effect at times.
"rendered" attribute is good enough to replace c:if tag and even if my code contains html tag along with jsf tag -- i prefer using <div> to show conditional display (i.e. accordian functionality) rather than JSTL for reasons mentioned above.

- Varun
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Ok, let's try this one more time with a bit more code:

[ May 15, 2005: Message edited by: Gregg Bolinger ]
Dhananjay Inamdar
Ranch Hand

Joined: Jan 27, 2003
Posts: 130
I solved this problem on the second day only. I will like to update you all with my solution. (If anybody interested)

As I mentioned in my problem, I am using one transfer object to display data in table. I wanted to provide hyperlink for all records of logged in user and remaining records I wanted to display read only.

I have a 'logged in username' stored in session and I am getting other user's name from database for every record. I added one more boolean attribute in this transfer object. I am setting this boolean variable true if 'logged in user = user' condition satisfies and else I am setting this value as false. So from back-end to the transfer object my records was setted with proper true/false values.

Now on the jsp page I made a following change

<h:commandLink action="#{trip.getDetails}" immediate="true" rendered="#{requestScope.tripto.createdbyUser}">
<hutputText value="#{tripto.tripId}" style="text-align:center" rendered="#{!requestScope.tripto.createdbyUser}"/>

So if condition stisfies then code execution will render first 2 lines as commandlink and if condition is not satisfaying then code will display only output text.

This satisfied my requirement. I hope it will help somebody in future.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

So basically you did what I suggested and called it your solution. Hmm. Well, glad it's working none-the-less. :roll:
[ May 31, 2005: Message edited by: Gregg Bolinger ]
Julia Roberts

Joined: Dec 21, 2005
Posts: 3
Hi there,
I am trying something similar but it is not working out. I have a datatable in which i need to display a colum conditionally as a commandlink and as a read-only. Here is my code:

<h anelGroup id="siPanel">
<t ataTable id="siDataTable" border="1"
rowClasses="gridStyle-tr-data, gridStyle-tr-alt-data"
columnClasses="colCenter, colLeft, colRight, colRight, colRight, colRight, colCenter"
value="#{siBean.defaultSIList}" rows="20"
<f:facet name="header">
<h utputText value=""/>
<h anelGroup rendered="#{siBean.isDefault}">
<h:commandLink action="#{siBean.setDefaultSI}" immediate="true">
<h utputText value="#{si.defaultHeader}" style="text-align:center" />
<t:updateActionListener property="#{siBean.instructionId}" value="#{si.instructionId}" />
<t:updateActionListener property="#{siBean.clientAccountNumber}" value="#{si.clientAccountNumber}" />
<t:updateActionListener property="#{siBean.clientSystemId}" value="#{si.clientSystemId}" />
<t:updateActionListener property="#{siBean.clientFirm}" value="#{si.clientFirm}" />
</h anelGroup>

<h anelGroup rendered="#{!siBean.isDefault}">
<h utputText value="#{si.defaultHeader}" style="text-align:center" />
</h anelGroup>

And the backing bean looks like:
public List<StandardInstructions> getDefaultSIList() {
if (defaultSIList == null) {
defaultSIList = Collections
.synchronizedList(new java.util.ArrayList<StandardInstructions>());
} else {
try {

CashwebDAO dao = CashwebDAO.getInstance();
StringBuffer where = new StringBuffer();
where.append(" where ");

if (null != getFromAccNoSearch()
&& !"null".equals(getFromAccNoSearch())
&& "true".equals(getFromAccNoSearch())) {
if(null != getClientAccountNumber()
&& !"null".equals(getClientAccountNumber())
&& !"".equals(getClientAccountNumber())){
where.append("si.account_no = '" + getClientAccountNumber() + "'");
System.out.println(" Inside if from acc no search ---------");
where.append(" and si.status = 'APPROVED' ");
where.append(" si.status = 'APPROVED' ");
dao.getDefaultSIs(new CashwebDAOBridgeAdapter() {
public void processSIs(ResultSet rs) throws SQLException {
System.out.println(" INSIDE processSIs ---------");
System.out.println(" rs size ---------" + rs.getFetchSize());
String beneficiaryType;
String dfltInstruction;
while ( {
StandardInstructions si = new StandardInstructions();
beneficiaryType = rs.getString(6);
if (beneficiaryType != null)
beneficiaryType = beneficiaryType.equals("Y") ? "Financial Institution"
: "Individual/Corporate";

dfltInstruction = rs.getString(9);
System.out.println(" dfltInstruction ---------" + dfltInstruction);
if (rs.wasNull() || dfltInstruction == null || dfltInstruction == "null" || dfltInstruction == "NULL" || dfltInstruction.trim().length() == 0) {
System.out.println(" setting default false---------" );
si.setDefaultHeader("Make Default");
System.out.println(" setting null---------" );
System.out.println(" setting true---------" );
}, where);
} catch (Exception exc) {
log("Exception getting the default SI list.", exc);
return defaultSIList;

But i get the error:
11:49:19,728 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
javax.faces.el.PropertyNotFoundException: Bean: com.daiwausa.cashweb.jsf.mbeans.SIBean, property: isDefault
at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(
at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(
at org.apache.myfaces.el.PropertyResolverImpl.getProperty(
at org.apache.myfaces.el.PropertyResolverImpl.getValue(
at org.apache.myfaces.el.ELParserHelper$MyPropertySuffix.evaluate(
at org.apache.commons.el.ComplexValue.evaluate(
at org.apache.myfaces.el.ValueBindingImpl.getValue(
at javax.faces.component.UIComponentBase.isRendered(
at org.apache.myfaces.renderkit.RendererUtils.renderChild(
at org.apache.myfaces.renderkit.RendererUtils.renderChildren(
at org.apache.myfaces.renderkit.RendererUtils.renderChild(
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.renderColumnBody(
at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.renderColumnBody(
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.encodeColumnChild(
at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.encodeColumnChild(
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.encodeChildren(
at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.encodeChildren(
at javax.faces.component.UIComponentBase.encodeChildren(
at org.apache.myfaces.renderkit.RendererUtils.renderChild(
at org.apache.myfaces.renderkit.RendererUtils.renderChildren(
at org.apache.myfaces.renderkit.html.HtmlGroupRendererBase.encodeEnd(
at javax.faces.component.UIComponentBase.encodeEnd(
at javax.faces.webapp.UIComponentTag.encodeEnd(
at javax.faces.webapp.UIComponentTag.doEndTag(
at org.apache.myfaces.taglib.UIComponentBodyTagBase.doEndTag(
at org.apache.jsp.defaultmanagement_jsp._jspx_meth_h_panelGroup_0(org.apache.jsp.defaultmanagement_jsp:285)
at org.apache.jsp.defaultmanagement_jsp._jspx_meth_h_form_0(org.apache.jsp.defaultmanagement_jsp:235)
at org.apache.jsp.defaultmanagement_jsp._jspx_meth_f_view_0(org.apache.jsp.defaultmanagement_jsp:187)
at org.apache.jsp.defaultmanagement_jsp._jspService(org.apache.jsp.defaultmanagement_jsp:129)
at org.apache.jasper.runtime.HttpJspBase.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.jasper.servlet.JspServletWrapper.service(
at org.apache.jasper.servlet.JspServlet.serviceJspFile(
at org.apache.jasper.servlet.JspServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.ApplicationDispatcher.invoke(
at org.apache.catalina.core.ApplicationDispatcher.processRequest(
at org.apache.catalina.core.ApplicationDispatcher.doForward(
at org.apache.catalina.core.ApplicationDispatcher.forward(
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(
at org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(
at org.apache.myfaces.lifecycle.LifecycleImpl.render(
at javax.faces.webapp.FacesServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(

Please help me

Thanks in advance,
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
To add some spice, JSF 1.1 or below and JSP are definitely not going well together. Things changed with the arrival of JSF 1.2 and JSP 2.1 where a lot of improvements in place to make the collaboration between JSF and JSP more harmony.

SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="" target="_blank" rel="nofollow"></a>
Sushant Bodke

Joined: Oct 29, 2009
Posts: 1
Thanks a lot for the solution, Gregg, I am new to JSF and this really helped!
I agree. Here's the link:
subject: Evaluating 2 expressions in JSF using JSTL if tag
It's not a secret anymore!