Thymeleaf不评估自定义方言/处理器

时间:2014-12-03 16:42:06

标签: spring-boot thymeleaf

我最近在我的spring-boot应用程序中添加了一些自定义方言和处理器,但当我将它们放在页面中时:

<!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>

并在浏览器中打开页面,标签未评估为正确的值。 FormDialect就是这样:

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

}

和FormProcessor是:

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我在我的网页配置中注册了Dialect:

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

其余的我尝试使用你的代码而Dialect仍在注册:

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

方言:

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

和处理器

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