File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Evaluating 2 expressions in JSF using JSTL if tag

 
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1400
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sushant Bodke
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the solution, Gregg, I am new to JSF and this really helped!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic