Kleber Mota
Kleber Mota

Reputation: 9065

Thymeleaf don't evaluate custom dialects / processors

I recently add some custom dialects and processor to my spring-boot application, but when I put them in the page like that:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:form="http://form" xmlns:field="http://field">
<body>
    <form:form>
    ...
    </form:form>
</body>
</html>

and open the page in the browser, the tags is not evaluated to the right value. The FormDialect is this:

public class FormDialect extends AbstractDialect {

  public FormDialect() {
    super();
  }

  //
  // All of this dialect's attributes and/or tags
  // will start with 'hello:'
  //
  public String getPrefix() {
    return "form";
  }


  //
  // The processors.
  //
  @Override
  public Set<IProcessor> getProcessors() {
    final Set<IProcessor> processor = new HashSet<IProcessor>();
    processor.add(new Form());
    return processor;
  }

}

and the FormProcessor is that:

public class Form extends AbstractProcessor {
  @Override
  public ProcessorResult doProcess(Arguments arguments,ProcessorMatchingContext context,Node node) {
    Element form = new Element("form");
    node.setProcessable(true);
    node.getParent().insertBefore(node, form);
    return ProcessorResult.OK;
  }

  @Override
  public int getPrecedence() {
    return 0;
  }

  @Override
  public IProcessorMatcher<? extends Node> getMatcher() {
    return new ElementNameProcessorMatcher("form");
  }
}

what I am doing wrong here?

Upvotes: 0

Views: 1265

Answers (1)

Pablo
Pablo

Reputation: 1682

I get the Dialect registered in my web configuration with:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public FormDialect formDialect() {
        return new FormDialect();
    }
}

For the rest I tried with your code and the Dialect is still registered:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:form="">
  <form:form>
  ...
  </form:form>
</html>

Dialect:

import java.util.HashSet;
import java.util.Set;
import org.pgg.photodb.web.thymeleaf.processor.Form;
import org.thymeleaf.dialect.AbstractDialect;
import org.thymeleaf.processor.IProcessor;

    public class FormDialect extends AbstractDialect {
        @Override
        public String getPrefix() {
            return "form";
        }

        @Override
        public Set<IProcessor> getProcessors() {
            final Set<IProcessor> processor = new HashSet<IProcessor>();
            processor.add(new Form());
            return processor;
        }
    }

and Processor

import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.processor.AbstractProcessor;
import org.thymeleaf.processor.ElementNameProcessorMatcher;
import org.thymeleaf.processor.IProcessorMatcher;
import org.thymeleaf.processor.ProcessorMatchingContext;
import org.thymeleaf.processor.ProcessorResult;

public class Form extends AbstractProcessor {
    @Override
    public ProcessorResult doProcess(Arguments arguments, ProcessorMatchingContext context, Node node) {
        Element form = new Element("form");
        node.setProcessable(true);
        node.getParent().insertBefore(node, form);

        return ProcessorResult.OK;
    }

    @Override
    public int getPrecedence() {
        return 0;
    }

    @Override
    public IProcessorMatcher<? extends Node> getMatcher() {
        return new ElementNameProcessorMatcher("form");
    }
}

Upvotes: 1

Related Questions