Results 1 to 6 of 6

Thread: How to keep sort order defined in the controller

  1. #1
    Join Date
    May 2005
    Posts
    11

    Default How to keep sort order defined in the controller

    I am very new to spring so please forgive me if my question has been answered. I need to maintain my order defined in my controller obrained from my database and display it as an orfered list of fields in my JSP. Currently in my controller I tried using a Hashmap and a Hashtable to return information to my view, but these structures don't maintain order.

    Map myModel = new Hashtable();
    ..
    ..
    ..
    myModel.put("category",category);
    myModel.put("scmid", scm);
    ..
    ..
    ..
    return new ModelAndView("details", "model", myModel);


    In my View i have the following:

    <core:forEach var='item' items='${model}'>
    <tr align=left valign=middle>
    <td>
    [<core:out value='${item.key}'/>]:
    </td>
    <td>
    [<core:out value='${item.value}'/>]
    </td>
    </tr>
    </core:forEach>

    How would I be able to make sure the display order is how I put the parameters in myModel?

    Thanks in advance

    Steven Headley

  2. #2
    Join Date
    Jul 2005
    Location
    Chicago
    Posts
    38

    Default

    Have you considered sotring this data in the command? This is the natural place to keep your data. You would be able to store any type of array then.

  3. #3
    Join Date
    Jul 2005
    Posts
    246

    Default

    Why not implement a List backed Map? It's easy and is something every developer should have in their toolbox IMO. I believe commons collections has something along these lines too, though I don't tend to use it.

    Code:
    /*
     * MPSC-Util - A library of miscellaneous common code
     * Copyright &#40;C&#41; 2005 Matt Parker
     * 
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
     * version 2.1 of the License, or &#40;at your option&#41; any later version.
     * 
     * This library is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
     * 
     * You should have received a copy of the GNU Lesser General Public
     * License along with this library; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     */
    package uk.co.mpcontracting.modules.util;
    
    import java.io.Serializable;
    import java.util.AbstractSet;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    
    /**
     * An implementation of a <code>Map</code> that is backed by a <code>java.util.ArrayList</code>
     *
     * @author Matt Parker &#40;matt@mpcontracting.co.uk&#41;
     */
    public class OrderedSet extends AbstractSet implements Serializable
    &#123;
        private List list;
        
        /**
         * Constructs an empty <code>OrderedSet</code>
         */
        public OrderedSet&#40;&#41;
        &#123;
            super&#40;&#41;;
            
            list = new ArrayList&#40;&#41;;
        &#125;
        
        /**
         * Constructs an empty <code>OrderedSet</code> with the specified initial capacity
         *
         * @param initialCapacity The initial capacity
         */
        public OrderedSet&#40;int initialCapacity&#41;
        &#123;
            super&#40;&#41;;
            
            list = new ArrayList&#40;initialCapacity&#41;;
        &#125;
        
        /**
         * Constructs a new <code>OrderedSet</code> containing the elements in the specified collection
         *
         * @param collection The specified collection
         */
        public OrderedSet&#40;Collection collection&#41;
        &#123;
            super&#40;&#41;;
            
            list = new ArrayList&#40;collection&#41;;
        &#125;
        
        /**
         * Returns an iterator over the elements in this set
         *
         * @return An iterator over the elements in this set
         */
        public Iterator iterator&#40;&#41;
        &#123;
            return &#40;list.iterator&#40;&#41;&#41;;
        &#125;
        
        /**
         * Returns the number of elements in this set
         *
         * @return The number of elements in this set
         */
        public int size&#40;&#41;
        &#123;
            return &#40;list.size&#40;&#41;&#41;;
        &#125;
        
        /**
         * Adds the specified object to this set if it isn't already present
         *
         * @param object The object to add to this set
         * @return Whether the set already contained the specified object
         */
        public boolean add&#40;Object object&#41;
        &#123;
            if &#40;!contains&#40;object&#41;&#41;
            &#123;
                return &#40;list.add&#40;object&#41;&#41;;
            &#125;
            else
            &#123;
                return &#40;false&#41;;
            &#125;
        &#125;
        
        /**
         * Retrieves the value stored at a given index. This is possible because the set
         * is backed with a <code>java.util.ArrayList</code>
         *
         * @param index The given index
         * @return The value stored at the given index
         */
        public Object get&#40;int index&#41;
        &#123;
            return &#40;list.get&#40;index&#41;&#41;;
        &#125;
    &#125;
    Code:
    /*
     * MPSC-Util - A library of miscellaneous common code
     * Copyright &#40;C&#41; 2005 Matt Parker
     * 
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
     * version 2.1 of the License, or &#40;at your option&#41; any later version.
     * 
     * This library is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
     * 
     * You should have received a copy of the GNU Lesser General Public
     * License along with this library; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     */
    package uk.co.mpcontracting.modules.util;
    
    import java.io.Serializable;
    import java.util.AbstractMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    /**
     * An implementation of a <code>Map</code> that is backed by an <code>OrderedSet</code>
     *
     * @author Matt Parker &#40;matt@mpcontracting.co.uk&#41;
     */
    public class OrderedMap extends AbstractMap implements Serializable
    &#123;
        private Set entrySet;
        
        /**
         * Constructs an empty <code>OrderedMap</code>
         */
        public OrderedMap&#40;&#41;
        &#123;
            super&#40;&#41;;
            
            entrySet = new OrderedSet&#40;&#41;;
        &#125;
        
        /**
         * Constructs an empty <code>OrderedMap</code> with the specified initial capacity
         *
         * @param initialCapacity The initial capacity
         */
        public OrderedMap&#40;int initialCapacity&#41;
        &#123;
            super&#40;&#41;;
            
            entrySet = new OrderedSet&#40;initialCapacity&#41;;
        &#125;
        
        /**
         * Constructs a new <code>OrderedMap</code> with the same mappings as the specified map
         *
         * @param map The specified map
         */
        public OrderedMap&#40;Map map&#41;
        &#123;
            this&#40;&#41;;
            
            putAll&#40;map&#41;;
        &#125;
        
        /**
         * Retrieves a collection of the entries stored in this map
         *
         * @return A collection of the entries stored in this map
         */
        public Set entrySet&#40;&#41;
        &#123;
            return &#40;entrySet&#41;;
        &#125;
        
        /**
         * Associates the given value with the specified key in this map. If the
         * map previously contained a mapping for this key, the old value is
         * replaced
         *
         * @param key The specified key
         * @param value The given value
         * @return The value previously associated with the specified key, or <code>null</code>
         * if there was no mapping for the key
         */
        public Object put&#40;Object key, Object value&#41;
        &#123;
            Object previousValue = null;
            Entry existingEntry = null;
            
            for &#40;Iterator iterator = entrySet.iterator&#40;&#41;; iterator.hasNext&#40;&#41;;&#41;
            &#123;
                existingEntry = &#40;Entry&#41;iterator.next&#40;&#41;;
                
                if &#40;&#40;key == null&#41; && &#40;existingEntry.getKey&#40;&#41; == null&#41;&#41;
                &#123;
                    previousValue = existingEntry.getValue&#40;&#41;;
                    break;
                &#125;
                else if &#40;&#40;key != null&#41; && &#40;key.equals&#40;existingEntry.getKey&#40;&#41;&#41;&#41;&#41;
                &#123;
                    previousValue = existingEntry.getValue&#40;&#41;;
                    break;
                &#125;
            &#125;
            
            if &#40;previousValue != null&#41;
            &#123;
                existingEntry.setValue&#40;value&#41;;
                
                return &#40;previousValue&#41;;
            &#125;
            else
            &#123;
                entrySet.add&#40;new SimpleEntry&#40;key, value&#41;&#41;;
                
                return &#40;null&#41;;
            &#125;
        &#125;
        
        /**
         * Retrieves the value stored at a given index. This is possible because the map
         * is backed with an <code>OrderedSet</code>
         *
         * @param index The given index
         * @return The value stored at the given index
         */
        public Object get&#40;int index&#41;
        &#123;
            Entry entry = &#40;Entry&#41;&#40;&#40;OrderedSet&#41;entrySet&#41;.get&#40;index&#41;;
            
            return &#40;entry.getValue&#40;&#41;&#41;;
        &#125;
        
        /**
         * A simple implementation of the <code>Entry</code> interface
         */
        private class SimpleEntry implements Entry, Serializable
        &#123;
            Object key;
            Object value;
            
            public SimpleEntry&#40;Object key, Object value&#41;
            &#123;
                this.key = key;
                this.value = value;
            &#125;
            
            public SimpleEntry&#40;Map.Entry entry&#41;
            &#123;
                key = entry.getKey&#40;&#41;;
                value = entry.getValue&#40;&#41;;
            &#125;
            
            public Object getKey&#40;&#41;
            &#123;
                return &#40;key&#41;;
            &#125;
            
            public Object getValue&#40;&#41;
            &#123;
                return &#40;value&#41;;
            &#125;
            
            public Object setValue&#40;Object value&#41;
            &#123;
                Object oldValue = this.value;
                
                this.value = value;
                
                return &#40;oldValue&#41;;
            &#125;
            
            public boolean equals&#40;Object object&#41;
            &#123;
                if &#40;!&#40;object instanceof Map.Entry&#41;&#41;
                &#123;
                    return &#40;false&#41;;
                &#125;
                
                Map.Entry entry = &#40;Map.Entry&#41;object;
                
                return &#40;eq&#40;key, entry.getKey&#40;&#41;&#41; &&  eq&#40;value, entry.getValue&#40;&#41;&#41;&#41;;
            &#125;
            
            public int hashCode&#40;&#41;
            &#123;
                return &#40;&#40;&#40;key == null&#41; ? 0 &#58; key.hashCode&#40;&#41;&#41; ^
                    &#40;&#40;value == null&#41; ? 0 &#58; value.hashCode&#40;&#41;&#41;&#41;;
            &#125;
            
            public String toString&#40;&#41;
            &#123;
                return &#40;key + "=" + value&#41;;
            &#125;
            
            private boolean eq&#40;Object object1, Object object2&#41;
            &#123;
                return &#40;&#40;object1 == null&#41; ? &#40;object2 == null&#41; &#58; object1.equals&#40;object2&#41;&#41;;
            &#125;
        &#125;
    &#125;

  4. #4
    Join Date
    Jul 2005
    Posts
    28

    Default

    Try using a LinkedHashMap

    It should iterate in the order of insertion.

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    The other uglier approach I sometimes use is to put all the keys in a List and then iterate over the list, retrieving stuff out of the map

  6. #6
    Join Date
    May 2005
    Posts
    11

    Default

    The LinkedHashMap works. Thanks for all the help guys

    Regards,

    Steve

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. hibernate pagination
    By oliverchua in forum Data
    Replies: 8
    Last Post: Sep 23rd, 2005, 06:06 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •