【问题标题】:Thymeleaf + Spring: get rid of the default element idThymeleaf + Spring:摆脱默认元素id
【发布时间】:2015-06-02 09:34:43
【问题描述】:

在 Thymeleaf (2.1.4.RELEASE) 中使用 th:field 时,有什么方法可以抑制元素的自动生成 ID 属性?例如,给定代码:

<input type="text" th:field="*{year}" />

将生成以下 HTML:

<input type="text" id="year" name="year" value="" />

我想要实现的是(没有id属性):

<input type="text" name="year" value="" />

在 JSP 中就像设置空 id 一样简单:

<form:input path="year" id="" />

但 Thymeleaf 只是将这个空属性替换为默认生成的属性。

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

    好的,我查看了 Thymeleaf (2.1.4.RELEASE) 的源代码,在 Spring 方言中负责设置元素 id 的方法是 org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor.doProcess(...) (source on Github) 调用 org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.computeId(...) (source on Github)。如果你看computeId(...),你会发现没有简单的方法来设置空id。

    所以我们需要用一种不简单的方式来做这件事:) 这里是:
    我创建了一个自定义方言并定义了一个自定义属性noid。标记如下所示:

    <input type="text" th:field="*{year}" thex:noid="true" />
    

    有一个很棒的tutorial 解释了如何在 Thymeleaf 中创建和使用自定义方言,下面是最重要的部分:负责从给定元素中删除 id 属性的属性处理器。

    需要注意的重要事项:

    • 高优先级值 (9999) 保证此处理器将作为最后一个处理器执行(因此在此处理器执行后其他处理器不会修改 id)
    • 修改类型设置为替换,所以我们完全替换了 id 元素的值
    • removeAttributeIfEmpty(...) 返回 true,不言自明,如果为空则删除属性
    • getModifiedAttributeValues(...) 设置 id 为空值,由于上述方法返回 true,id 属性被删除

    代码:

        public class NoIdAttrProcessor extends AbstractAttributeModifierAttrProcessor {
    
            public NoIdAttrProcessor() {
                super("noid");
            }
    
            @Override
            public int getPrecedence() {
                return 9999;
            }
    
            @Override
            protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
                return ModificationType.SUBSTITUTION;
            }
    
            @Override
            protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
                return true;
            }
    
            @Override
            protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) {
                return false;
            }
    
            @Override
            protected Map<String, String> getModifiedAttributeValues(Arguments arguments, Element element, String attributeName) {
                Map<String, String> values = new HashMap<>(1);
                values.put("id", "");
                return values;
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      如果您不想在输入字段的 id 中使用它,只需将值分配给 th:name 字段,

      <input type="text" th:name="*{year}" />
      

      会给你这样的输出,

      <input type="text" name="2015" />
      

      或者你可以在末尾使用一个字符串,使生成的id和name属性不同,像这样

      <input type="text" th:name="*{year}" th:id="*{year} + '-year' " />
      

      会给你输出,

      <input type="text" name="2015" id="2015-year"/>
      

      【讨论】:

      • 问题是我希望 name 属性在字面上等同于 th:field(即 *{year} -> year、*{foo} -> foo 等),因为这是Spring 数据绑定所需的。同样设置 name="year" 也不是解决方案,因为 th:field 也会自动设置 value 属性。
      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 2014-08-15
      • 1970-01-01
      • 2017-08-26
      • 2021-09-01
      • 2016-09-05
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多