我的表格声明如下:
<form id="reply-form-frm" novalidate="novalidate" action="/reply/responsive" method="POST">
[@spring.bind "model.form"/]
[@spring.bind "model.form.message" /]
<label for="${spring.status.expression}" class="clearfix" data-error="Please enter a message">
<span class="label-text">Your message</span>
<span class="set-right required-flag">Required</span>
</label>
<p class="input-error[#if spring.status.errorMessages?size > 0] reveal-error[#else] hide-error[/#if]">
[#if spring.status.errorMessages?size > 0]
<span>[#list spring.status.errorMessages as error]${error}[/#list]</span>
[/#if]
</p>
[@spring.formTextarea "model.form.message", 'aria-required="true" required="required" data-clear="true"' /]
</form>
其他一些字段以相同的方式呈现。表单收到的模型是:
model: {
form: {
senderName: "hahahahaha",
senderEmail: "asd@asd.es",
advertId: 1000011793,
optInMarketing: false,
advertClickSource: "natural"
}
我的控制器有这种映射方法:
public ModelAndView sendReplyResponsive(@ModelAttribute("model.form") ReplyForm replyForm,
BindingResult bindingResult) {
//TODO: content irrelevant ...
}
问题是,当表单呈现时,元素映射到“form.message”而不是“model.form.message”,我的控制器无法在帖子上找到ReplyForm对象。
关于如何强制Spring使用“model.form.message”作为名称的任何想法?
答案 0 :(得分:1)
简短回答 - 没有必要强迫使用弹簧&#34; model.form.message&#34;正如名字一样。
答案很长:
Spring总是删除名称的第一部分(&#34; model&#34;在你的情况下)它不依赖于表示引擎,这个逻辑在org.springframework.web.servlet.support.BindStatus#BindStatus
内编码。常见的方法是使用相同的模型进行渲染和绑定,在这种情况下,您应该:
1.使用&#34;模型&#34;绑定类:
public ModelAndView sendReplyResponsive(@ModelAttribute("model") ReplyModel model, BindingResult bindingResult) {
ReplyForm replyForm = model.getForm();
//TODO: content irrelevant ...
}
或2.将ReplyForm公开为独立模型属性:
<form id="reply-form-frm" novalidate="novalidate" action="/reply/responsive" method="POST">
[@spring.bind "form.message" /]
<label for="${spring.status.expression}" class="clearfix" data-error="Please enter a message">
<span class="label-text">Your message</span>
<span class="set-right required-flag">Required</span>
</label>
<p class="input-error[#if spring.status.errorMessages?size > 0] reveal-error[#else] hide-error[/#if]">
[#if spring.status.errorMessages?size > 0]
<span>[#list spring.status.errorMessages as error]${error}[/#list]</span>
[/#if]
</p>
[@spring.formTextarea "form.message", 'aria-required="true" required="required" data-clear="true"' /]
</form>
模型将如下:
model: {
form: {
senderName: "hahahahaha",
senderEmail: "asd@asd.es",
advertId: 1000011793,
optInMarketing: false,
advertClickSource: "natural"
}
},
form: {
senderName: "hahahahaha",
senderEmail: "asd@asd.es",
advertId: 1000011793,
optInMarketing: false,
advertClickSource: "natural"
}
如果您的&#34;型号&#34;只包含表格然后没有理由保留它。 而控制器方法将是:
public ModelAndView sendReplyResponsive(@ModelAttribute("form") ReplyForm replyForm,
BindingResult bindingResult) {
//TODO: content irrelevant ...
}