Spring form option list

In the previous three articles (Spring MVC forms with select drop down list, Generifing MVC spring select lists and Generic model for form select I have shown how to develop a generic dropdown list with Spring 3.0 MVC and web forms. With the heavy stuff off our backs we can focus on the details such as the order of the option list.
In the example we always used a HashMap as the model for our option list. Often you wish to control the order of your list e.g. if you have a list with absence:

1 day, 2 days, 3 days, … 2 weeks, …

It would be very awkward if the list is not in the natural order. Natural here means a humanly sensible ordering.
Using a HashMap does not provide us with the desired result: The ordering in a HashMap is based on the hash codes of the keys. What we want instead is a SortedMap with a key that implements the Comparable interface.
My prime choice therefore is TreeMap<Integer,String>. Other Comparable classes that can be used out of the box are:
BigDecimal, BigInteger, Byte, ByteBuffer, Character, CharBuffer, Charset, CollationKey, Date, Double, DoubleBuffer, File, Float, FloatBuffer, IntBuffer, Long, LongBuffer, ObjectStreamField, Short, ShortBuffer, String and URI.

Schreibe einen Kommentar