Thymeleaf + Spring: get rid of the default element id

dominik

Is there any way to suppress auto-generating ID attribute for elements while using th:field in Thymeleaf (2.1.4.RELEASE)? For example, given code:

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

will produce the following HTML:

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

What I want to achieve is (no id attribute):

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

In JSP it was as easy as setting empty id:

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

but Thymeleaf just replaces this empty attribute with the default-generated one.

dominik

Ok, I have looked inside the source code of Thymeleaf (2.1.4.RELEASE) and the method responsible for setting element id in Spring dialect is org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor.doProcess(...) (source on Github) that calls org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.computeId(...) (source on Github). If you look at computeId(...), you will see that there is no simple way to set empty id.

So we need to do it in not a simple way :) Here it is:
I created a custom dialect and defined a custom attribute noid. The markup looks like this:

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

There is a great tutorial explaining how to create and use custom dialects in Thymeleaf and below is the most important part: attribute processor responsible for removing id attribute from given element.

Important things to note:

  • high precedence value (9999) guarantees that this processor will be executed as the last one (so no other processors will modify id after this one is executed)
  • modification type is set to substitution so we are completely replacing value of id element
  • removeAttributeIfEmpty(...) returns true, rather self-explanatory, remove attribute if empty
  • getModifiedAttributeValues(...) sets id to empty value and because above-mentioned method returns true, id attribute is removed

Code:

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

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot get validation working with Spring Boot and Thymeleaf

Spring Thymeleaf collapse table with th:id

How to get rid of JpaRepository interfaces in Spring Boot

how to override default thymeleaf resolver in spring boot

Vcl Style Utils - Get rid of default form icon

How to get rid off the "default" property when loading modules with SystemJS?

How to get ID of currently logged in user using spring security and Thymeleaf

How to get rid of the default background of the Android DatePickerDialog in Android 4.4.4?

Post-Redirect-Get with Spring WebFlux and Thymeleaf

How to get rid of first element of store id using pandas?

"Get" document from cosmosdb by id (not knowing the _rid)

How to get rid of multiple branch_id?

Get element or default value?

How do I get rid of a default input binding?

how to get rid of default white "space" around an element?

Yosemite: can I get rid of the default finder view for the home directory?

make wmaker default window manager - get rid of Cinnamon

How to get rid of the default blue color on hover over a button?

Thymeleaf + Spring MVC Post/Redirect/Get

Facebook bot get rid of default message

Spring security authentication How to get rid of

How to get rid of this element or change its color?

How to get rid of default overhang in Scenegraph Developer extensions

How to get rid of space below element inside details element

Trying to get rid of whitespace to left of ul element

How to get rid of the default window in QT5

get rid of default blue in Xamarin forms

How do I get rid of the default macOS menu items in wxWidgets?

Is there a way to get rid of the default file manager for Xubuntu without having to get rid of the desktop?