spring 禁用输入的模型绑定
spring model binding with disabled input
抱歉问了一个愚蠢的问题,但我不太明白发生了什么,如果这是我怀疑的..好吧,我真的很茫然。
我正在使用 spring boot + thymeleaf + materialize css 来显示和验证表单。
现在我在很多例子中都没有遇到过这种情况:
一些表单字段是预填的,对客户来说应该是禁用的,显示它们的预填值。这个预填充发生在控制器中,而我处理一些其他请求,并重定向到这个视图
我正在使用 th:object 将 pojo 绑定到表单
<form id="register_form" action="#" th:action="@{/showform}" th:object="${userInfo}" method="post">
<div class="input-field">
<label th:text="#{label.surname}" for="surname"></label>
<input type="text" th:field="*{surname}" id="surname" th:attr="value=${userInfo.surname}" />
</div>
<div class="input-field">
<label th:text="#{label.name}" for="givenname"></label>
<input type="text" th:field="*{givenname}" id="givenname" th:attr="value=${userInfo.givenname}" disabled="disabled" />
</div></form>
并像这样在控制器的 POST 处理程序中获取它:
@RequestMapping(value = {"/showform"}, method = RequestMethod.POST)
public ModelAndView submitFormPage(@ModelAttribute("userInfo") @Valid UserInfo userInfo,
BindingResult bindingResult, RedirectAttributes redir)
{
ModelAndView mview = new ModelAndView();
if (bindingResult.hasErrors())
{
// show form again with error messages
mview.addObject("userInfo", userInfo);
mview.setViewName("/showform");
}
else
{
// ...
}
return mview;
}
RedirectAttributes 是出于其他原因。如您所见,表单上有两个元素,第一个启用,第二个禁用。
它们的值正确填充了来自 POJO 的预填充值,我通过 ModelMap 传递给视图。我也可以在 GET 处理程序中跟踪它。
但是我从视图中返回的 ModelMap 包含前面提到的 POJO,其中 NULL 值代替了绑定到禁用控件的元素。我希望它们由 value 属性的内容填充,即使这些控件被禁用。启用的控件可以正常携带它们的值。
或者仅仅是禁用的控件没有包含在回传中?如果是这样的话,你会建议我怎么做?一些人建议添加一个模糊的 CSS,这将 "fake" 禁用控件的行为。还是我在一般布线中遗漏了什么?
我对可能的变通办法感到恐惧 - 但我一定是做错了什么.. th:attr 是我尝试过的变通办法之一,但它似乎并没有奏效。我也试过使用 th:id 和 th:disabled 但它也没有帮助。
这里有个误区我想到disabled
的使用。
A readonly
element is just not editable, but gets sent when the
according form submits. a disabled
element isn't editable and isn't
sent on submit. Another difference is that readonly
elements can be
focused (and getting focused when "tabbing" through a form) while
disabled
elements can't.
More detailed comparison
所以回答你的问题:如果你想将你的属性绑定到你的 pojo
而用户仍然无法编辑它们,你应该选择 readonly
。
抱歉问了一个愚蠢的问题,但我不太明白发生了什么,如果这是我怀疑的..好吧,我真的很茫然。 我正在使用 spring boot + thymeleaf + materialize css 来显示和验证表单。 现在我在很多例子中都没有遇到过这种情况:
一些表单字段是预填的,对客户来说应该是禁用的,显示它们的预填值。这个预填充发生在控制器中,而我处理一些其他请求,并重定向到这个视图
我正在使用 th:object 将 pojo 绑定到表单
<form id="register_form" action="#" th:action="@{/showform}" th:object="${userInfo}" method="post">
<div class="input-field">
<label th:text="#{label.surname}" for="surname"></label>
<input type="text" th:field="*{surname}" id="surname" th:attr="value=${userInfo.surname}" />
</div>
<div class="input-field">
<label th:text="#{label.name}" for="givenname"></label>
<input type="text" th:field="*{givenname}" id="givenname" th:attr="value=${userInfo.givenname}" disabled="disabled" />
</div></form>
并像这样在控制器的 POST 处理程序中获取它:
@RequestMapping(value = {"/showform"}, method = RequestMethod.POST)
public ModelAndView submitFormPage(@ModelAttribute("userInfo") @Valid UserInfo userInfo,
BindingResult bindingResult, RedirectAttributes redir)
{
ModelAndView mview = new ModelAndView();
if (bindingResult.hasErrors())
{
// show form again with error messages
mview.addObject("userInfo", userInfo);
mview.setViewName("/showform");
}
else
{
// ...
}
return mview;
}
RedirectAttributes 是出于其他原因。如您所见,表单上有两个元素,第一个启用,第二个禁用。 它们的值正确填充了来自 POJO 的预填充值,我通过 ModelMap 传递给视图。我也可以在 GET 处理程序中跟踪它。
但是我从视图中返回的 ModelMap 包含前面提到的 POJO,其中 NULL 值代替了绑定到禁用控件的元素。我希望它们由 value 属性的内容填充,即使这些控件被禁用。启用的控件可以正常携带它们的值。
或者仅仅是禁用的控件没有包含在回传中?如果是这样的话,你会建议我怎么做?一些人建议添加一个模糊的 CSS,这将 "fake" 禁用控件的行为。还是我在一般布线中遗漏了什么?
我对可能的变通办法感到恐惧 - 但我一定是做错了什么.. th:attr 是我尝试过的变通办法之一,但它似乎并没有奏效。我也试过使用 th:id 和 th:disabled 但它也没有帮助。
这里有个误区我想到disabled
的使用。
A
readonly
element is just not editable, but gets sent when the according form submits. adisabled
element isn't editable and isn't sent on submit. Another difference is thatreadonly
elements can be focused (and getting focused when "tabbing" through a form) whiledisabled
elements can't.
More detailed comparison
所以回答你的问题:如果你想将你的属性绑定到你的 pojo
而用户仍然无法编辑它们,你应该选择 readonly
。