2 Replies Latest reply on Apr 6, 2005 4:33 PM by treespace

    Using enums on JSP pages

      Constant management is a major sore point that can be addressed using static final constants accessed via snipplets. It's ugly but you get compile time checking versus flakey runtime symptoms from typos.

      Enums might improve things but 3.2.6 jasper is using javac without the -source 1.5 switch. EL looks like a no-go since you'll lose the compile-time check.

      Altnerative ideas/solutions welcome.

        • 1. Re: Using enums on JSP pages

          I have come up with a solution so elegant it brings tears to my eyes:)

          Color is an enum. I convert it to a map that can be useBean'd and dereferenced in EL. So my app uses the enum while the JSP uses the same enum in map form. Sweet!

          public class ColorMap<String, String> extends TreeMap {
          public ColorMap() {
          for(Color value : Color.values()) {
          put(value.name(), value.name());
          }
          }
          }

          Usage: note that EL does map lookups using dot notation making for some very clean syntax:

          <useBean id="colors" class="ColorMap"...

          ...



          <input type="submit" value="${colors.Seafoam}" ...


          • 2. Re: Using enums on JSP pages

            Correction:

            public class ColorMap extends TreeMap<String, String> {
             public ColorMap() {
             for(Color value : Color.values()) {
             put(value.name(), value.name());
             }
             }
            }