为什么不使用 Spring 的表单标签库?http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-jsp-formtaglib
taglib(与您的控制器结合)自动映射您的 ModelAttribute。在对表单执行 GET 请求时,您会创建 PostRequest 的新(可能是空的)对象并将其粘贴到模型中。在发布表单后,spring 会为您提供带有表单值的 ModelAttribute。
示意图示例:
控制器:
@RequestMapping(value="/path", method = RequestMethod.GET)
public String initForm(ModelMap model) {
PostRequest pr = new PostRequest();
model.addAttribute("command", pr);
return "[viewname]";
}
@RequestMapping(value="/path", method = RequestMethod.POST)
public ModelAndView postForm(
@ModelAttribute("command") PostRequest postRequest) {
// postRequest should now contain the form values
logger.debug("username: " + postRequest.getUsername());
return "[viewname]";
}
jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form method="post" enctype="utf8">
Username: <form:input path="username" />
<br/>
<%-- ... --%>
<input type="submit" value="Submit"/>
</form:form>