Java 如何使用boostrap select在每个spring mvc3的选择列表中设置所选项目?
我有以下查询参数:Java 如何使用boostrap select在每个spring mvc3的选择列表中设置所选项目?,java,html,jsp,twitter-bootstrap,spring-mvc,Java,Html,Jsp,Twitter Bootstrap,Spring Mvc,我有以下查询参数: http://localhost:8085/myPage?selectListId=2 我已经从GET方法中的查询字符串中剥离了“selectListId”参数,我想将下拉列表中的选定项设置为该索引值 我正在使用引导,所以不确定这是否重要。该列表由我从spring mvc3控制器传入的viewModel填充 <form class="form-horizontal" action="myController/indexSubmit" method="post">
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>
${item}
提交
如何设置此值(最好不使用javascript)?谢谢 像那样使用
id
是个坏主意,因为它不是真正的标识符,只是当前索引。为了回答这个问题,给出了一个处理表单提交的控制器方法
@RequestMapping(/* some mapping */
public String saveSelection(@RequestParam("selectList") String selectListId, Model model) {
model.addAttribute("selectListId", selectList);
return "form"; // whatever it is
}
将当前索引与请求属性中的索引进行比较,如果它们匹配,则将其设置为选中
<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>
${item}
提交
您是否在询问如何循环查看添加到控制器中的模型中的列表元素?嗨……不,我有这部分内容……列表填充得很好。但是,在回发时,我将所选项目存储在查询参数中…使用列表下拉列表重定向回主页。。。但是想将下拉列表中的项目索引设置为选中。太棒了!第一次尝试就成功了!再次感谢!