2014-10-17 20 views
0

我使用Spring表單功能在Spring MVC中創建了一個Web應用程序。當我提交jsp表單時,我從控制器上的多個選擇框中獲取數據作爲字符串數組,然後存儲在數據庫中。當用戶編輯記錄時,我如何在多個選擇框中顯示選定的選項。使用後端bean的Spring MVC多重選擇

此圖用於填充多個選擇框。

Map mp = new HashMap(); 
mp.put("111", "test1"); 
mp.put("112", "test2"); 
mp.put("113", "test3"); 
mp.put("114", "test4"); 
mv.addObject("cat", mp); 

此圖是用戶選擇的列表,從db中獲取。

Map selMap = new HashMap(); 
selMap.put("111", "test1"); 
selMap.put("114", "test4"); 
mv.addObject("selcat", selMap); 

如何在編輯頁面上按相同的順序顯示test1和test4。 我需要這種格式的結果。 enter image description here

回答

0

我會給你比如使用Spring註釋: 這將作爲您referenceData的形式。

@ModelAttribute("cat") 
public List<String> referenceData(){ 
    Map mp = new HashMap(); 
    mp.put("111", "test1"); 
    mp.put("112", "test2"); 
    mp.put("113", "test3"); 
    mp.put("114", "test4"); 

    return mp; 
} 

現在您可以選擇在多個拖放數據值下降爲

@RequestMapping(method=RequestMethod.GET) 
    public String showForm(ModelMap map){ 
     StudentCommand command =new StudentCommand(); 
     Map selMap = new HashMap(); 
     selMap.put("111", "test1"); 
     selMap.put("114", "test4"); 
     command.setCat(selMap); 
     map.addAttribute("command", command); 
     return "studentregistration"; 
    } 

JSP

<form:select multiple="true" path="cat" items="${cat}" itemLabel="cat" itemValue="something" /> 
+0

感謝@Shoaib您的快速回復,它沒有顯示選定的項目。 – AKV 2014-10-17 07:43:55