【问题标题】:Thymeleaf th:field model evaluationThymeleaf th:现场模型评估
【发布时间】:2014-07-24 16:38:31
【问题描述】:

我正在使用自定义 Thymeleaf 属性处理器将动态内容包含到视图中,该处理器在处理属性本身的同时简单地添加其他节点。

我使用的代码与下面的非常相似:

final Template template = arguments.getTemplateRepository().getTemplate(
            new TemplateProcessingParameters(arguments.getConfiguration(), "name-of-a-view", arguments.getContext()));
final List<Node> children = template.getDocument().getChildren();

// Add to the tree.
for (final Node node : children) {
    element.addChild(node);
}

这很好用,但是当包含的节点包含使用 th:object 和 th:field 的表单时会中断。

我将我需要的模型放在节点变量映射中,实际上 th:object 确实找到并检索了对象,但 th:field 似乎并不关心并以 a 中断

Neither BindingResult nor plain target object for bean name 'model' available as request attribute

根据我的理解(逐步调试),在我看来 th:field 只在请求上下文中搜索模型。

我错过了什么吗?

提前谢谢你。

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    不,你是正确的。我仍然不确定为什么 th:field 的绑定与其他 th: 属性不同,但它的工作方式肯定不同。本质上,除非您的 th:object 在模型上,否则您不能使用 th:field。解决方法是停止使用 th:field 并手动指定输入属性,例如:

    <form action="#" th:action="@{/process}" th:object="${objectFromList}" method="post">
      <input type="text" id="fieldName" name="fieldName" th:value="*{fieldName}" />   
    </form>
    

    我意识到这篇文章已经过时了。希望这对遇到这种怪癖的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-01-02
      • 2019-11-30
      • 2017-03-24
      • 2020-03-06
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      相关资源
      最近更新 更多