spring中使用form标签「建议收藏」

2022-07-28 10:23:06 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

在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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128278.html原文链接:https://javaforall.cn

0 人点赞