wood burning stoves 2.0*
The moose likes JSP and the fly likes How to test for a character in a JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to test for a character in a JSTL "test"" Watch "How to test for a character in a JSTL "test"" New topic
Author

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

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

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 ]


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

I would still convert the character to a string before sending it to the JSP page.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to test for a character in a JSTL "test"
 
Similar Threads
JSTL c:when
Conditional includes
Nesting JSTL Tags
JSTL Conditional
Can multiple conditions be checked using JSTL?