gpt4 book ai didi

java - Controller 中未检测到 freemarker 形式的 Spring 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:20 25 4
gpt4 key购买 nike

我有一个声明如下的表单:

  <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"
}

我的 Controller 有这个映射方法:

 public ModelAndView sendReplyResponsive(@ModelAttribute("model.form") ReplyForm replyForm,
BindingResult bindingResult) {
//TODO: content irrelevant ...
}

问题是,当表单呈现时,元素映射到“form.message”而不是“model.form.message”,并且我的 Controller 在发布时找不到 ReplyForm 对象。

关于如何强制 Spring 使用“model.form.message”作为名称有什么想法吗?

最佳答案

简短回答 - 不可能强制 spring 使用“model.form.message”作为名称。

长答案:Spring 总是删除名称的第一部分(在您的情况下为“模型”),它不依赖于表示引擎,此逻辑在 org.springframework.web.servlet.support.BindStatus#BindStatus 内编码。常见的方法是使用相同的模型进行渲染和绑定(bind),在您的情况下您应该:
1.使用“model”类进行绑定(bind):

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"
}

如果您的“模型”仅包含表单,则没有理由保留它。 Controller 方法将是:

public ModelAndView sendReplyResponsive(@ModelAttribute("form") ReplyForm replyForm,
BindingResult bindingResult) {
//TODO: content irrelevant ...
}

关于java - Controller 中未检测到 freemarker 形式的 Spring 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22696359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com