2013-08-28 26 views
0

我有以下查詢參數:如何使用boostrap select在每個彈簧mvc3的選擇列表中設置選定的項目?

http://localhost:8085/myPage?selectListId=2 

我在GET方法去皮,切開的「selectListId」參數從查詢字符串,我想設置我的下拉列表中該指標值的選擇項。

我使用引導程序,所以不知道是否重要。該列表從我從spring mvc3控制器傳入的viewModel中填充。

<form class="form-horizontal" action="myController/indexSubmit" method="post"> 
    <select name="selectList" class="form-control" placeholder=".input-medium" height> 
     <c:forEach items="${viewModel.getlistitems()}" var="item" varStatus="count"> 
      <option value="${count.index}">${item }</option> 
     </c:forEach> 
    </select> 
    <button type="submit" class="btn btn-primary btn-medium">Submit</button> 
</form> 

我該如何設置此值(最好不要使用javascript)?謝謝!

+0

你問如何控制器方法循環遍歷你添加到控制器中'Model'列表的列表元素? –

+0

嗨...不,我有這個部分...這個清單填充得很好。但是,在回發中,我將選定的項目存儲在查詢參數中...重定向回主頁面,列表下拉列表...但想要設置具有所選項目索引的下拉列表。 – JaJ

回答

2

使用這樣的id是一個壞主意,因爲它不是一個真正的標識符,它只是當前的索引。要知道答案的緣故,因爲處理您的表單提交

@RequestMapping(/* some mapping */ 
public String saveSelection(@RequestParam("selectList") String selectListId, Model model) { 
    model.addAttribute("selectListId", selectList); 
    return "form"; // whatever it is 
} 

你比較當前索引的索引在請求屬性,並將其設置selected它們是否匹配

<form class="form-horizontal" action="myController/indexSubmit" method="post"> 
    <select name="selectList" class="form-control" placeholder=".input-medium" height> 
     <c:forEach items="${viewModel.getlistitems()}" var="item" varStatus="count"> 
      <option value="${count.index}" ${not empty selectListId && selectListId == count.index ? 'selected' : ''} >${item }</option> 
     </c:forEach> 
    </select> 
    <button type="submit" class="btn btn-primary btn-medium">Submit</button> 
</form> 
+0

太棒了!第一次嘗試!再次感謝! – JaJ

+0

@JaJ你很受歡迎! –

相關問題