开发者

Should I use a Java Enum to Fetch user for a specific year?

I need to fetch user for a specific year in a web app built using Seam 2.2 and I was wondering if it would be reasonable to use a java Enum to build the list of year choices to render in the front end where the user would pick one of them, probably in a jsf h:selectOneMenu list element.

So do you think it is recommended to use a java Enum to build this kind of list?

Or should I use a simple List to hold the options?

The specific requirement is to build a list of the current and past 30 years so the user would pick one.

[]s

As mentioned in the comments I went to a simple solution using List. Follows the code I am using for that, as I am using Seam I have just constructed a Factory which builds the last 50 year list and than in xhtml I reference that and show in a combo list to the end user select from it.

Factory code:

    @Factory("listaUltimos50Anos")
    public List<Integer> listaUltimos50Anos()
    {
        ArrayList<Integer> ultimos50Anos = new ArrayList<Integer>();
        GregorianCalendar hoje = new GregorianCalendar();
        hoje.setTime(new java.util.Date());
        int anoAtual = hoje.get(Calendar.YEAR);
        for(int i = 0; i <= 50;开发者_JAVA技巧 i++)
        {
            ultimos50Anos.add(anoAtual);
            anoAtual -= 1;
        }
        return ultimos50Anos;
    }

and than in xhtml I reference this factory:

<s:decorate id="dataField" template="../layout/edit.xhtml">
    <ui:define name="label">Ano</ui:define>
    <h:selectOneMenu id="ano" value="#{carroHome.instance.ano}" required="true">
        <s:selectItems var="anos" value="#{listaUltimos50Anos}" label="#{anos}" />
    </h:selectOneMenu>
</s:decorate> 

[]s


Why would you want to enumerate a set consequative set of numbers?

The purpose of enum is to turn something intrinsicly non numeric like a state or status code into a number for the sake of efficiency and readability (although I have always found the readability argument doubtful). Another common and valid use is to turn a widly disperesed set of numbers like sql codes into a simpler sequence of numbers.


i would just create a simple list of Integers, from currentYear - 30 to currentYear .. iterate this years and create the options list with value = current iteration

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜