How to populate dropdown box in Spring MVC

后端 未结 3 2166
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 10:14

I have been trying to find out how to populate a dropdown box in Spring MVC. There are a few threads out there on this subject but none of them that I have found have helped

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 11:13

    I have solved this kind of problem today by myself. This is very simple and easy to understand. In Spring MVC 3.0 controller just place this code -

     @ModelAttribute("creditCardTypes")
     public Map populateCreditCardTypes() {
            Map creditCardTypes = new LinkedHashMap();
            creditCardTypes.put("VS", "Visa");creditCardTypes.put("MC", "MasterCard");
            creditCardTypes.put("AE", "American Express");
            creditCardTypes.put("DS", "Discover");creditCardTypes.put("DC", "Diner's Club");                
            return creditCardTypes;
        }
    

    Now "creditCardTypes" attribute will be avaiable in the page loading or page submitting scope , means it will available whatever the requestmapping url would be.

    In jsp , place this code inside the - Credit card types:

    
        
        
     
    

    here , path="creditCardType" means the attribute in the Spring MVC model/command object, items="${creditCardTypes}" means all the populated credit card types will be available in "creditCardTypes" ModelAttribute. Thats it !!!

提交回复
热议问题