1

我有以下 ViewModel 设置要在我的下拉列表中显示的项目列表。

我的控制器/索引

// 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)如何获取所选项目的实际字符串文字而不是索引?

谢谢!

4

1 回答 1

1
model.addAttribute("nameOfList", viewModel.getlistitems());

然后在您的表单中使用弹簧标签库

<form:select path="modelPath">
    <form:option value="0" label="Select an Option" />
    <form:options items="${nameOfList}" />
</form:select>
于 2013-08-28T06:55:20.773 回答