我有以下ViewModel设置要在我的下拉列表中显示的项目列表。
myController的/索引
// set view model list of items to populate in drop down...viewModel contains listItems as
List<String>
viewModel.setlistitems(//list retrieved 3rd party);
// add view model to model to display items in drop down list viewModel.getlistitems() returns List<String>
model.addAttribute("viewModel", viewModel);
然后我在index.jsp中有下拉列表,它可以很好地填充列表:
<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>
提交帖子,我在“selectList”中得到所选下拉列表的索引。但是,当我尝试从我的视图模型中获取列表项时,它为空?
@RequestMapping(value="indexSubmit", method = RequestMethod.POST)
public String indexSubmit( @RequestParam String selectList,
@ModelAttribute("viewModel") ViewModel viewModel, ModelMap model) {
System.out.println("Selected Item: " + selectList); // returns the index fine
System.out.println("Items: " + viewModel.getlistitems()); // returns NULL!! this was the same list call that populated the drop down
return "redirect:/index";
}
我如何1)从我的视图模型中返回list<string>
以获取要通过所选项索引引用的项列表以作为查询参数返回或2)如何获取实际的字符串文字选定的项目而不是索引?
THX!
答案 0 :(得分:1)
model.addAttribute("nameOfList", viewModel.getlistitems());
然后在你的表单中使用spring标签lib
<form:select path="modelPath">
<form:option value="0" label="Select an Option" />
<form:options items="${nameOfList}" />
</form:select>