iterating over Enum constants in JSP

后端 未结 2 1507
长情又很酷
长情又很酷 2021-01-04 03:52

I have an Enum like this

package com.example;

public enum CoverageEnum {

    COUNTRY,
    REGIONAL,
    COUNTY
}

I would like to iterate

2条回答
  •  清歌不尽
    2021-01-04 04:37

    If you are using Tag Libraries you could encapsulate the code within an EL function. So the opening tag would become:

    
    

    EDIT: In response to discussion about an implementation that would work for multiple Enum types just sketched out this:

    public static > Enum[] getValues(Class klass) {
        try { 
            Method m = klass.getMethod("values", null);
            Object obj = m.invoke(null, null);
            return (Enum[])obj;
        } catch(Exception ex) {
            //shouldn't happen...
            return null;
        }
    }
    

提交回复
热议问题