webtier@glassfish.java.net

JSPs, Enums, and toString

From: Hilco Wijbenga <hilco.wijbenga_at_gmail.com>
Date: Mon, 5 Apr 2010 18:20:44 -0700

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