Link to home
Start Free TrialLog in
Avatar of girionis
girionisFlag for Greece

asked on

JSTL - compare a Character type value

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.
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

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
Avatar of girionis

ASKER

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.
<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
>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
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?
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 :)
should be..
<%
String myVar = myBean.getVar();
%>
<c:when test="${<%=myVar%> == '1'}">
     <c:out value="test"/>
</c:when>
ASKER CERTIFIED SOLUTION
Avatar of searlas
searlas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
searlas  is right. His second and third  entries work for me.   rrz
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 :)
Avatar of searlas
searlas

rrz, thanks for confirming it works.

girionis, Did it work for you?

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
I just tried your second example and it and it works like a charm. Thank you :)
Actually I tried the third example and it works but I believe rrz@871311 so I take it the second one works as well.