点击提交后,我无法显示所选的下拉值。
<form action="#" th:action="@{/page1/send}" th:object="${val}" method="post">
<select class="form-control" name="action" id="action" th:value="${value}">
<option th:selected="'ABC' == ${value}">ABC</option>
<option th:selected="'XYZ' == ${value}">XYZ</option>
</select>
</form>
在controller.java中:
@RequestMapping("/page1/send")
public String send(Values val, Model model) {
String value = val.getValue();
model.addAttribute("value",value);
return "page1";
}
当我选择XYZ并提交时,它又恢复为ABC!
答案 0 :(得分:0)
当我选择XYZ并提交
我在您的表单中看不到“提交”按钮。但是我明白你的要求。假设page1.html首先是通过以下某些控制器方法加载的
@GetMapping(path = "/")
public ModelAndView home()
{
ModelAndView mav = new ModelAndView();
mav.addObject("value", "XYZ");
mav.setViewName("page1");
return mav;
}
这可提供以下html页面形式
<form action="#" th:action="@{/page1/send}" method="post">
<select class="form-control" name="action" id="action">
<option th:selected="'ABC' == ${value}" value="ABC">ABC</option>
<option th:selected="'XYZ' == ${value}" value="XYZ">XYZ</option>
</select>
<input type="submit" value="submit"/>
</form>
现在,我可以读取提交的下拉列表的值,并将其作为
从控制器方法发送回页面@RequestMapping("/page1/send")
public String testSubmit(@RequestParam("action") String action, Model model) {
model.addAttribute("value",action);
return "page1";
}
让我知道是否有帮助。