在 Spring MVC 中,将表单中的一个字段仅发送到服务器的最佳方式是什么?我需要发送选择框值,但我也希望选择框预先填充正确的值。
通常我会有一些表单支持对象,并将他绑定(bind)到表单,但是当我只有一个字段要发送时,我不需要任何表单支持对象。但是我不能使用 form:form 和 form:select 进行绑定(bind),因为它需要指定表单支持对象中的字段。
谢谢。
最佳答案
在你的 jsp/ View 中,使用经典的 html <form/>
和 <select/>
:
<form id="form" method="POST">
<select id="selected" name="selected">
<option value="1">First value</option>
<option value="2">Second value</option>
</select>
</form>
在您的 Controller 中,此方法将获取 selected
表单提交完成时的值:
@RequestMapping(method=RequestMethod.POST)
public String submitForm(@RequestParam String selected) {
// your code here!
return "nextView";
}
要预填充选择框,您必须手动将值从 Controller 传递到 View ,最后使用 JSTL(或任何您使用的)/javascript 选择它。
关于java - Spring MVC 一种输入字段形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735101/