【问题标题】:spring-boot don't evaluate tags based on custom thymeleaf dialectsspring-boot 不会根据自定义 thymeleaf 方言评估标签
【发布时间】:2014-12-09 15:42:42
【问题描述】:

在我当前的 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

任何人都可以看到我在这里缺少什么?

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案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();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 2021-08-27
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多