Solved

JSTL - compare a Character type value

Posted on 2004-04-16
14
3,665 Views
Last Modified: 2013-12-03
Hello,

I have a bean that has a Character type property named "documentType". I try to compare its value using JSTL but to no avail. I have read that JSTL does not support explicitly the Character object but that it should be converted to String using standard Java conversion rules. However the following fails:

<c:when test="${doc.documentType == '1'}">
      <c:out value="test"/>
</c:when>

where "doc" is my bean that has the "documentType" variable. I have verified that the value is "1" using a <c:out value="${doc.documentType}"/> statement right before the comparison.

The only solution I have found is to have a method that converts the Character to String and returns it but I am wondering if there is any other solution.

Thank you.
0
Comment
Question by:girionis
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10842184
is it a must that you use jtls

cause you can do the same with jsp

<%

while(doc.getDocumentType.equals("1")) {
   out.println("test");
}

%>

you might have to add this method to your java bean

public String getDocumentType() {
   return documentType;
}

that will work
cheers,
Ghost
0
 
LVL 35

Author Comment

by:girionis
ID: 10842509
Hello copyPasteGhost, yes your comment is a possible solution and I thought about that too but the whole application is using JSTL and I wouldn't want to break the design.I am looking for a JSTL-only solution, if there is any.
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10842594
<c:when test="${doc.documentType() == '1'}">
     <c:out value="test"/>
</c:when>

maybe you need to call the method in your javabean

try that..note I added the () at the end of documentType
Ghost
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Expert Comment

by:rrz
ID: 10843847
>looking for a JSTL-only solution, if there is any.    
So, I guess using a scriptet is cheating then. Anyway this the only way I could make it work.  

<%
   request.setAttribute("myC",new Character('x'));
%>
<c:choose>
<c:when test="${doc.documentType == requestScope.myC}">
     Yes
</c:when>
<c:otherwise>NoNoNo</c:otherwise>
</c:choose>
rrz
0
 
LVL 35

Author Comment

by:girionis
ID: 10844803
copyPasteGhost

> doc.documentType()

This fails as the property accessed should be a variable and not a method.

rrz@871311

yes scriplet is a possible solution as copyPasteGhost also suggested but I would like to only use JSTL. Any more thoughts?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10845963
you can do this..

<%
String myVar = myBean.getVar();
%>
<c:when test="${<%=myVar=> == '1'}">
     <c:out value="test"/>
</c:when>

that's kinda cheating...I know..
But that will work too :)
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10845965
should be..
<%
String myVar = myBean.getVar();
%>
<c:when test="${<%=myVar%> == '1'}">
     <c:out value="test"/>
</c:when>
0
 
LVL 7

Accepted Solution

by:
searlas earned 53 total points
ID: 10846408
I'd think one of these has to work:

<c:when test="${doc.documentType == 1}">
     <c:out value="test"/>
</c:when>

<c:when test="${doc.documentType == '49'}">
     <c:out value="test"/>
</c:when>

<c:when test="${doc.documentType == 49}">
     <c:out value="test"/>
</c:when>

(the 49 in the last two comes from casting the char '1' to a number - i.e. it's ASCII value)
0
 
LVL 27

Expert Comment

by:rrz
ID: 10847672
searlas  is right. His second and third  entries work for me.   rrz
0
 
LVL 35

Author Comment

by:girionis
ID: 10849220
I have to admin I never thought of using the ASCII value instead. Let me try it at work on Monday and I will get back to you :)
0
 
LVL 7

Expert Comment

by:searlas
ID: 10876613
rrz, thanks for confirming it works.

girionis, Did it work for you?

0
 
LVL 35

Author Comment

by:girionis
ID: 10876785
Hello searlas, sorry haven't tried it yet, my colleague found a work around and he changed it before I got to the office. I haven't forgotten the question though. When I have some time I will try it and I will let you know.

Regards
0
 
LVL 35

Author Comment

by:girionis
ID: 10877106
I just tried your second example and it and it works like a charm. Thank you :)
0
 
LVL 35

Author Comment

by:girionis
ID: 10877121
Actually I tried the third example and it works but I believe rrz@871311 so I take it the second one works as well.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Good resource for accessibility implementation 1 71
stackato and cloud 4 85
by zero exception 10 39
Java string replace 11 43
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now