Thymeleaf定制方言不起作用

时间:2018-04-27 10:20:11

标签: java spring spring-boot thymeleaf

我的处理器自定义方言没有解析任何值,我不知道为什么。在生成的视图中,$ {content}应该没有任何内容,并且在将标记更改为th:text后会出现。我使用的是Spring Boot v1.5.9.RELEASE,Spring v4.3.13.RELEASE

pom.xml依赖项(它的子模块)

<properties>
    <h2.version>1.4.194</h2.version>
    <java-version>1.8</java-version>
    <org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>
    <org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
    <thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>${org.thymeleaf-version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>${org.thymeleaf-version}</version>
    </dependency>
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        <version>${thymeleaf-layout-dialect.version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
        <version>${org.thymeleaf.extras-version}</version>
    </dependency>
    <!--WebJars-->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <!--database-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
        <version>${h2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
</dependencies>

LineSeparatorProcessor.java

public class LineSeparatorProcessor extends AbstractAttributeTagProcessor {

    private static final String ATTR_NAME = "lstext";
    private static final int PRECEDENCE = 10000;


    public LineSeparatorProcessor(final String dialectPrefix) {
        super(
                TemplateMode.HTML,
                dialectPrefix,
                null,
                false,
                ATTR_NAME,
                true,
                PRECEDENCE,
                true);
    }


    protected void doProcess(
            final ITemplateContext context, final IProcessableElementTag tag,
            final AttributeName attributeName, final String attributeValue,
            final IElementTagStructureHandler structureHandler) {

        final IEngineConfiguration configuration = context.getConfiguration();

        final IStandardExpressionParser parser =
                StandardExpressions.getExpressionParser(configuration);

        final IStandardExpression expression = parser.parseExpression(context, attributeValue);

        final String value = (String) expression.execute(context);

         structureHandler.setBody(
                HtmlEscape.escapeHtml5Xml(value).replace(System.getProperty("line.separator"), "<br />"),
                false);

    }
}

MyDialect.java

public class MyDialect extends AbstractProcessorDialect {

    public MyDialect() {
        super(
                "MyDialect",
                "mydialect",
                13000);
    }

    public Set<IProcessor> getProcessors(final String dialectPrefix){
        final Set<IProcessor> processors = new HashSet<>();
        processors.add( new LineSeparatorProcessor(dialectPrefix) );
        return processors;
    }

}

ThymeleafConfiguration.java

@Configuration
public class ThymleafConfiguration {

    @Bean
    public MyDialect myDialect() {
        return new MyDialect();
    }
}

view.html

        <span mydialect:lstext="${content}" ></span>

1 个答案:

答案 0 :(得分:1)

您需要将方言添加到TemplateEngine的实例中。例如:

@Bean
public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setEnableSpringELCompiler(true);
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new MyDialect());
    return templateEngine;
}

您可以在Say Hello! Extending Thymeleaf in 5 minutes指南中找到此文档。