Skip to main content

[webtier] JSPs, Enums, and toString

3 replies [Last post]
Anonymous

Hi all,

I ran into something unexpected and I'd like to understand what's
happening. I'm using JDK1.5 (I also tried JDK1.6) and JSP 2.1 (and
Servlet 2.5) on Jetty 6.1.22.

I have an enum Name (note the overridden toString):

package org.example.web;
public enum Name {
NAME("the name");

private final String name;

private Name(final String name) {
this.name = name;
}

@Override
public String toString() {
System.out.println("HERE!!!!");
return name;
}
}

In my JSP I have:

:
${name.class}: ${name}
:

I'm using Spring Web MVC so in my controller I do:

model.put("name", Name.NAME);

At runtime I get:

class org.example.web.Name: NAME

So the overridden Name.toString seems to be completely ignored. When
adding an extra statement in my controller:

System.out.println(Name.NAME);

I get exactly what you'd expect in the console logging:

HERE!!!!
the name

So I think my code is correct. Why does the JSP not display correctly?
This should work, shouldn't it?

Cheers,
Hilco

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kchung
Offline
Joined: 2004-05-06
Points: 0

According to EL 2.2 spec, 1.18.2, when a Enum is coerced to a String, Enum.name() is used. One may argue that toString() should be used instead, but the current behavior conforms to the spec.

Hilco Wijbenga

On 6 April 2010 15:09, wrote:
> According to EL 2.2 spec, 1.18.2, when a Enum is coerced to a String, Enum.name() is used.  One may argue that toString() should be used instead, but the current behavior conforms to the spec.
> [Message sent by forum member 'kchung']

Wow, indeed, it says so in EL2.1 too. Thanks!

Mind you, this does not make sense to me. Without an explicit
toString(), Java uses the result of name() anyway. And name() can not
be overridden as it's final. So why not simply always use the default
Java behaviour and use toString()?

Using toString() is more intuitive (it's what a Java developer would
expect), simplifies the spec (fewer rules, no difference between Enums
and Objects), *and* it's much more flexible.

Do you remember why Enums are treated specially? (Given your user id,
I'm assuming you're Kin-Man Chung, one of the editors?) Is it too late
to change this?

Cheers,
Hilco

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

kchung
Offline
Joined: 2004-05-06
Points: 0

I agree with you! I wasn't involved in the original spec.

However EL 2.2 is already out. I filed a issue

https://uel.dev.java.net/issues/show_bug.cgi?id=14

to track it. Hopefully this will be addressed in the next EL spec.

Thanks for reporting it!