wood burning stoves 2.0*
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
Author

Evaluating 2 expressions in JSF using JSTL if tag

Dhananjay Inamdar
Ranch Hand

Joined: Jan 27, 2003
Posts: 130
Hi,

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}"/>
</h:commandLink>
</c:if>

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

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
Jasmina


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: 15299
    
    6

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 TripMBean.java

public boolean compareUser(String userName, String createdBy){
if(userName.equalsIgnoreCase(createdBy))
return true;
else
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}"/>
</h:commandLink>
</c:if>
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" />
</c:if>

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: 15299
    
    6

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





JSTL AND JSF ARE A NO NO
[ 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}">
</h:commandLink>
<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: 15299
    
    6

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
Greenhorn

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:

JSP:
<h anelGroup id="siPanel">
<t ataTable id="siDataTable" border="1"
width="100%"
styleClass="gridStyle"
rowClasses="gridStyle-tr-data, gridStyle-tr-alt-data"
columnClasses="colCenter, colLeft, colRight, colRight, colRight, colRight, colCenter"
value="#{siBean.defaultSIList}" rows="20"
renderedIfEmpty="false"
var="si"
preserveDataModel="true">
<h:column>
<f:facet name="header">
<h utputText value=""/>
</f:facet>
<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:commandLink>
</h anelGroup>

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

And the backing bean looks like:
public List<StandardInstructions> getDefaultSIList() {
if (defaultSIList == null) {
defaultSIList = Collections
.synchronizedList(new java.util.ArrayList<StandardInstructions>());
} else {
defaultSIList.clear();
}
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' ");
}else{
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 (rs.next()) {
StandardInstructions si = new StandardInstructions();
si.setInstructionId(rs.getInt(1));
si.setState(rs.getString(2));
si.setClientAccountNumber(rs.getString(3).trim());
si.setDeliveryMethod(rs.getString(4));
si.setNumberOfParties(rs.getInt(5));
beneficiaryType = rs.getString(6);
if (beneficiaryType != null)
beneficiaryType = beneficiaryType.equals("Y") ? "Financial Institution"
: "Individual/Corporate";

si.setBeneficiaryName(beneficiaryType);
si.setClientSystemId(rs.getString(7));
si.setClientFirm(rs.getString(8));
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---------" );
si.setDefaultNumber(null);
setDefault(false);
//setDefault(false);
}else{
si.setDefaultHeader("Default");
System.out.println(" setting true---------" );
si.setDefaultNumber("true");
setDefault(true);
}
defaultSIList.add(si);
}
}
}, 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(PropertyResolverImpl.java:445)
at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(PropertyResolverImpl.java:416)
at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:379)
at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:71)
at org.apache.myfaces.el.ELParserHelper$MyPropertySuffix.evaluate(ELParserHelper.java:532)
at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
at org.apache.myfaces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:378)
at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:822)
at org.apache.myfaces.renderkit.RendererUtils.renderChild(RendererUtils.java:436)
at org.apache.myfaces.renderkit.RendererUtils.renderChildren(RendererUtils.java:427)
at org.apache.myfaces.renderkit.RendererUtils.renderChild(RendererUtils.java:448)
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.renderColumnBody(HtmlTableRendererBase.java:195)
at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.renderColumnBody(HtmlTableRenderer.java:211)
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.encodeColumnChild(HtmlTableRendererBase.java:168)
at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.encodeColumnChild(HtmlTableRenderer.java:161)
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:154)
at org.apache.myfaces.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:94)
at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.encodeChildren(HtmlTableRenderer.java:57)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:319)
at org.apache.myfaces.renderkit.RendererUtils.renderChild(RendererUtils.java:444)
at org.apache.myfaces.renderkit.RendererUtils.renderChildren(RendererUtils.java:427)
at org.apache.myfaces.renderkit.html.HtmlGroupRendererBase.encodeEnd(HtmlGroupRendererBase.java:62)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:331)
at javax.faces.webapp.UIComponentTag.encodeEnd(UIComponentTag.java:349)
at javax.faces.webapp.UIComponentTag.doEndTag(UIComponentTag.java:253)
at org.apache.myfaces.taglib.UIComponentBodyTagBase.doEndTag(UIComponentBodyTagBase.java:55)
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(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:415)
at org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(JspViewHandlerImpl.java:234)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:352)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

Please help me

Thanks in advance,
Julia
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="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Sushant Bodke
Greenhorn

Joined: Oct 29, 2009
Posts: 1
Thanks a lot for the solution, Gregg, I am new to JSF and this really helped!
 
Don't get me started about those stupid light bulbs.
 
subject: Evaluating 2 expressions in JSF using JSTL if tag