Spring @ModelAttribute 不关心 commandName
JSP:
<form:form commandName="editWeather" method="post" action="../edit">
<!-- Input fields -->
<input type="submit" value="Submit">
</form:form>
这就是我在 Spring 中获取模型的方式:
@ModelAttribute("DONTGIVEADAMN") Weather weather
我仍然可以使用天气
来执行我的操作,并且效果很好,例如:
weatherService.editWeather(weather);
我的问题是......为什么这有效?
JSP:
<form:form commandName="editWeather" method="post" action="../edit">
<!-- Input fields -->
<input type="submit" value="Submit">
</form:form>
And this is how I get the model in Spring:
@ModelAttribute("DONTGIVEADAMN") Weather weather
And I can still use the weather
to do my operations and it works great, for example:
weatherService.editWeather(weather);
My question is...Why does this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当绑定从表单接收的数据时,模型属性名称并不重要(因为表单字段的名称对应于模型对象的字段名称),它仅在呈现表单时才重要。
特别是,当您的
POST
处理程序方法中的模型属性名称与表单中的commandName
不匹配时,您将能够接收数据,但不会能够重新显示带有验证错误的表单。Model attribute name doesn't matter when binding data received from a form (because names of form fields correspond to the names of fields of the model object), it matters only when rendering a form.
I particular, when model attribute name in your
POST
handler method doesn't match thecommandName
in the form, you will be able to receive the data, but won't be able to redisplay a form with validation errors.它匹配类类型(或接口),而不是变量/参数的名称;并且指定的请求映射/方法签名必须正确。
its matching the class type (or interface), not the name of the variable/parameter; and the specified request mapping/method signature must be correct.