在jsp头部包含:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
按如下方式编写表单:
<form:form method="post" action="addContact">
<table>
<tr>
<td><form:label path="firstname">111</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">222</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="lastname">333</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="lastname">444</form:label></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="555"/>
</td>
</tr>
</table>
</form:form>
从spring中调用有form标签的jsp页面,不能用普通方式调用,否则会出类似下面错误:
Neither BindingResult nor plain target object for bean name 'command' available as request attribute
使用ModelAndView调用就能成功得到页面:
@RequestMapping(value = "/contact", method = RequestMethod.GET)
public ModelAndView contact(Locale locale, Model model) {
return new ModelAndView("contact", "command", new Contact());
}
command是缺省值,如果你想修改自己指定值,需要在<form:form method="post" action="addContact">中添加commandName="xxx",例如:
<form:form method="post" action="addContact" commandName="xxx">
参考文档:
http://tntxie.iteye.com/blog/416121
http://viralpatel.net/blogs/2010/07/spring-3-mvc-handling-forms.html
本文介绍如何在Spring MVC中使用form标签创建表单,并通过ModelAndView进行页面调用,解决请求参数获取问题。
492

被折叠的 条评论
为什么被折叠?



