当前位置: 首页 > 知识库问答 >
问题:

Thymeleaf定制方言不起作用

房学
2023-03-14

我的自定义方言与处理器不解析任何值,我不知道为什么。在生成的视图中,${Content}应该在的地方没有任何东西,在将标签更改为th: text后,它会出现。我使用Spring Boot v1.5.9。发布,Springv4.3.13。发布

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>

LineSeparator处理器。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);

    }
}

我的方言。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;
    }

}

胸腺onfiguration.java

@Configuration
public class ThymleafConfiguration {

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

看法html

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

共有1个答案

滕英奕
2023-03-14

您需要将方言添加到模板引擎的实例中。例如:

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

您可以在“打招呼”中找到此文档!在5分钟的指导下扩展Thymeleaf。

 类似资料:
  • 上面的代码生成一个复杂的带有标签、输入字段和错误块的div块。 我想简化这个语法。我的想法是用一个自定义标记创建一个自定义方言,并写如下: 第二个更容易阅读,它清楚地向设计者表明这是一个特殊的元素。除此之外,更改主题会更容易,因为我只需要更改标记处理器中具体的片段位置,而不是数百个th:replace值。

  • 问题内容: 我正在尝试编写一个断言,以检查用户提供的大小是否为正值,如果不是,则使其为正,此语句位于类构造函数内部,该类构造函数采用大小值,然后生成array [size]。我写了下面的代码,我认为是正确的。 尽管我似乎从未评估过我的断言并继续执行程序,但会导致NegativeArraySize错误(我正在尝试避免) 我也尝试过 并且程序无法停止为负值。 我最近在Mac上运行Java时遇到了一些问

  • 和UserServiceImpl: 在前端,我创建了一个简单的表,如下所示: 现在,当我启动我的应用程序并单击删除按钮时,我在网站上出现了这样的错误:当前线程没有实际事务可用的EntityManager-不能可靠地处理“Remove”调用;嵌套异常是javax.persistence.TransactionRequiredException:当前线程没有实际事务可用的EntityManager-不

  • POM 应用程序 控制器 此外,我有模板文件夹中的资源文件夹和内部错误。html和index.html 当我访问localhost:8080/index时,会显示错误,而不是索引。我做错了什么?这真的是最简单的设置,它已经错了。。。

  • 我最近为Thymeleaf编写了一个自定义方言以及一个自定义处理器,以处理一些自定义标记,并在某些情况下用不同的标记替换它们,但我在编写处理器测试时遇到了问题: 类需要重写 方法,这是我需要测试的方法。 因为我的处理器涉及从获取变量,所以我尝试模拟它;但是,、和类都声明为final,这意味着它们不能被Mockito模拟。 我真的不想实例化一个实际的对象,因为它依赖于其他5个无法模拟的对象,我最终会

  • 主要内容:1. 什么是标准方言?,2. 标准表达式语法,2.7 表达式预处理,3. 基本的属性本节将带您了解一些最重要的概念,以了解以标准或SpringStandard方言编写的Thymeleaf模板。 1. 什么是标准方言? Thymeleaf是非常非常可扩展的,它允许自定义的名字来定义一组模板属性(或者甚至是标签),用自定语法评估计算表达式和应用逻辑。它更像是一个模板引擎框架。 它还带有一些称为标准方言(称为Standard和SpringStandard)的东西,它们定义了一组功能,这