spring-boot不会根据自定义的百日咳方言来评估标签

时间:2014-12-09 15:42:42

标签: spring spring-boot thymeleaf

在我目前的spring-boot应用程序中,我有这个配置为thymeleaf:

@Configuration
public class Thymeleaf {
  @Bean
  public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine  =  new SpringTemplateEngine();

    final Set<IDialect> dialects = new HashSet<IDialect>();
    dialects.add( new SpringSecurityDialect() );
    dialects.add( new FormDialect() );
    dialects.add( new FieldDialect() );
    engine.setDialects( dialects );

    return engine;
  }
}

我添加了这两种方言:

org.store.custom.thymeleaf.dialect.FormDialect.java

public class FormDialect extends AbstractDialect {

  public FormDialect() {
    super();
  }

  //
  // All of this dialect's attributes and/or tags
  // will start with 'form:'
  //
  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;
  }

}

org.store.custom.thymeleaf.dialect.FieldDialect.java

public class FieldDialect extends AbstractDialect {

  public FieldDialect() {
    super();
  }

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


  //
  // The processors.
  //
  @Override
  public Set<IProcessor> getProcessors() {
    final Set<IProcessor> processor = new HashSet<IProcessor>();
    processor.add(new Checkbox());
    processor.add(new DataList());
    processor.add(new Input());
    processor.add(new Property());
    processor.add(new Radio());
    processor.add(new Select());
    processor.add(new Textarea());
    return processor;
  }

}

但是当我在html页面上使用带有这些方言的标签时,此标签不会被评估为普通标签。该页面的代码如下:

<!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=""
  xmlns:field="">
<head>
  <title>Cadastro</title>
</head>
<body>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">Cadastrar <span th:text="${command.getClass().getSimpleName()}"/></h3>
    </div>
    <div class="panel-body">
      <form:form>
        <field-box th:each="item : ${command.getClass().getDeclaredFields()}">
          <div th:each="item2 : ${item.getDeclaredAnnotations()}">
            <div th:switch="${item2.annotationType().getSimpleName()}">
              <div th:case="'Checkbox'"><field:checkbox/></div>
              <div th:case="'DataList'"><field:datalist/></div>
              <div th:case="'Input'"><field:input/></div>
              <div th:case="'Radiobutton'"><field:radio/></div>
              <div th:case="'Select'"><field:select/></div>
              <div th:case="'Textarea'"><field:textarea/></div>
            </div>
          </div>
        </field-box>
      </form:form>
    </div>
  </div>
  <script src="js/form_submit.js"></script>
  <script src="js/form_valida.js"></script>
</body>
</html>

并且处理器的代码是这样的:

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

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

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

在这一刻,我的主要想法是因为我没有使用正确的名称空间值。我尝试这个值:

* http://form and http://field
* http://org for both
* http://org.store.custom.thymeleaf for both
* http://www.thymeleaf.org/org for both

任何人都可以看到我在这里缺少的东西吗?

1 个答案:

答案 0 :(得分:1)

当我将Thymeleaf类更改为此时,此问题得以解决:

@Configuration
public class Thymeleaf {
  @Autowired
  private FormDialect formDialect;

  @Autowired
  private FieldDialect fieldDialect;

  @Bean
  public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine  =  new SpringTemplateEngine();

    final Set<IDialect> dialects = new HashSet<IDialect>();
    dialects.add( new SpringSecurityDialect() );
    dialects.add( formDialect );
    dialects.add( fieldDialect );
    engine.setDialects( dialects );

    return engine;
  }

  @Bean
  public FormDialect formDialect() {
    return new FormDialect();
  }

  @Bean
  public FieldDialect fieldDialect() {
    return new FieldDialect();
  }
}