How to populate dropdown box in Spring MVC

后端 未结 3 2167
没有蜡笔的小新
没有蜡笔的小新 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:08

    Not sure what Controller method is called to show your view with documentNumberList, but you need to add that collection to the model passed to this view:

    model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers());
    

    Though from your exception stack trace you also missed an @Autowired on documentService field.

    0 讨论(0)
  • 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<String,String> populateCreditCardTypes() {
            Map<String,String> creditCardTypes = new LinkedHashMap<String,String>();
            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:

    <form:select path="creditCardType">
        <option value="Select" label="Select a card type"></option>
        <form:options items="${creditCardTypes}" />
    </form:select> 
    

    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 !!!

    0 讨论(0)
  • 2020-12-15 11:21
    @ModelAttribute("numberList")
    public List<Document> documentNumberList(){
        List<LabelValue> selectItems = new ArrayList<LabelValue>();
        List<Document> docList = documentService.retrieveAllDocumentNumbers();
        for (Document doc : docList) {
         selectItems.add(new LabelValue(doc.id,doc.value));
    }
        return selectItems;
    }
    

    FYI LabelValue class is a simple DTO that we use to carry the drop down label and value items. It will have a label and value attribute, and the corresponding getters/setters.

    LabelValue.java

     private String lable;
     private String value;
    
     //getters/setters
    

    ---- JSP -----

    <tr>
        <td>DocumentNumber</td>
        <td><form:select id="docNo" path="document_number">
               <form:option value="NONE" label="--- Select ---" />
               <form:options items="${numberList}" itemValue="value" itemLabel="lable"/>
             </form:select>
        </td>
        <td><form:errors path="document_number" cssClass="error" /></td>
    </tr>
    

    hope this helps..

    0 讨论(0)
提交回复
热议问题