Forums Register Login

How to test for a character in a JSTL "test"

+Pie Number of slices to send: Send
How do you do a character test in JSTL?

I have an expression like this:



The temp.value has a single character value like 'H' or 'L'.

However, the above c:when test gives this error: "javax.servlet.jsp.el.ELException: An exception occurred trying to convert String "H" to type "java.lang.Long"

So, how exactly to you do a character comparison in JSTL?

Thanks in advance.

mike

+Pie Number of slices to send: Send
What I did when I ran into that problem was to convert the character ('H') into its Unicode code-point (72) and use that instead:

Cryptic? Obscure? Not self-documenting? Guilty on all counts, but it works. But I'm never going to write a Java bean which returns a char ever again. One-character strings are much better.
+Pie Number of slices to send: Send
I"ve never used characters in the EL, and personally, I'd just make temp.value a single-character srting and be done with it. But you can grab the EL Specification -- it contains exhaustive rules for its type coersions.

[Edit: great minds think alike ]
+Pie Number of slices to send: Send
 

Paul Clapham wrote:What I did when I ran into that problem was to convert the character ('H') into its Unicode code-point (72) and use that instead:

Cryptic? Obscure? Not self-documenting? Guilty on all counts, but it works. But I'm never going to write a Java bean which returns a char ever again. One-character strings are much better.



Paul,

Woops....I'm not sure I understood your reply.

In my situation, the character is coming from the database. I can't convert it there. There's an Oracle column with a single H or L in it and Hibernate is returning the character to me in the application.

I did your test above and it "worked", that is, it displayed a a single row of data, but I'm not sure that's all I needed to do. I'm not sure what you meant by "converting" the 'H' character. Did you just mean doing the comparison as you showed it above (implicit conversion)?

Thanks in advance for the clarification.

-- mike
+Pie Number of slices to send: Send
 

Mike London wrote:I'm not sure what you meant by "converting" the 'H' character. Did you just mean doing the comparison as you showed it above (implicit conversion)?



Sorry, I meant me (the programmer) hunting down an ASCII chart on the internet, finding that 'H' was represented by 72 in ASCII (and hence in Unicode), and changing my (i.e. your) EL code to use 72 instead of 'H'. So then yeah, future programmers are going to look at that and say "72? What?" (or words to that effect). Code comments would help here.
+Pie Number of slices to send: Send
I would still convert the character to a string before sending it to the JSP page.
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:I would still convert the character to a string before sending it to the JSP page.



For me that would require rewriting my bean's method to return String rather than char. Which wouldn't have been that hard, but it wasn't something I wanted to mess with while converting to EL. It sounds like Mike's beans are being automatically generated by Hibernate, or something like that, but I would definitely recommend not working with chars in EL if you can avoid it.
+Pie Number of slices to send: Send
If it's a Hibernate entity, then I would add an @Tranisent getter that returns it in String format just for the EL. This is a tactic I've used before when what makes Hibernate happy isn't suitable for EL consumption.
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5342 times.
Similar Threads
JSTL c:when
Nesting JSTL Tags
JSTL Conditional
Can multiple conditions be checked using JSTL?
Conditional includes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:23:08.