Spring MVC一个输入字段表单

B.G*_*ill 4 java spring-mvc

Spring MVC中最好的方式是只向表单中的一个字段发送一个字段?我需要发送选择框值,但我也希望选择框预先填充正确的值.

通常我会有一些表单支持对象,并将他绑定到表单,但是当我只有一个字段要发送时,我不需要任何表单支持对象.但是我没有使用form:form和form:select for binding因为它需要指定表单后备对象中的字段.

谢谢.

jel*_*ies 14

在你的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>
Run Code Online (Sandbox Code Playgroud)

在您的控制器中,此方法将selected在表单提交完成时获取值:

@RequestMapping(method=RequestMethod.POST)
public String submitForm(@RequestParam String selected) {
    // your code here!
    return "nextView";
}
Run Code Online (Sandbox Code Playgroud)

要预填充选择框,您必须手动将值从控制器传递到视图,最后使用JSTL(或任何您使用的)/ javascript选择它.