hel*_*rth 0 spring materialize thymeleaf
对于一个愚蠢的问题感到抱歉,但我无法完全理解会发生什么,如果这是我怀疑的问题,那么我真的很茫然。我正在使用弹簧靴+百里香叶+物化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>
Run Code Online (Sandbox Code Playgroud)
并将其放入控制器的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;
}
Run Code Online (Sandbox Code Playgroud)
出于其他一些原因,存在RedirectAttributes。如您所见,表单上有两个元素,第一个被启用,第二个被禁用。它们的值已正确填入POJO中的预填充值,然后通过ModelMap传递到视图。我也可以在GET处理程序中跟踪它。
但是我从视图返回的ModelMap包含上述POJO,该POJO具有NULL值,而不是绑定到禁用控件的元素。我希望它们将由value属性的内容填充,即使这些控件已禁用。启用的控件保留其值。
还是只是禁用的控件根本不包含在回发中?如果是这样,您将如何建议我这样做?一些建议增加一个模糊的CSS,以“伪造”禁用控件的行为。还是我错过了常规布线中的某些内容?
我认为可能有变通办法,但我肯定做错了什么。th:attr是我尝试过的变通办法之一,但似乎并没有解决问题。我也尝试使用th:id和th:disabled,但这也没有帮助。
| 归档时间: |
|
| 查看次数: |
2444 次 |
| 最近记录: |