gpt4 book ai didi

java - Thymeleaf 自定义方言不起作用

转载 作者:行者123 更新时间:2023-12-02 11:19:05 27 4
gpt4 key购买 nike

我的带有处理器的自定义方言不解析任何值,我不知道为什么。在生成的 View 中,${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();
}
}

查看.html

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

最佳答案

您需要将方言添加到 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 中找到此记录。指南。

关于java - Thymeleaf 自定义方言不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50060300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com