springmvc配置thymeleaf视图解析器

  1. 引入thymeleaf的依赖
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring5</artifactId>
      <version>3.0.13.RELEASE</version>
    </dependency>

2. 配置springmvc视图解析

<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="order" value="1"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="templateEngine">
        <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
            <property name="templateResolver">
                <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    
                    <!-- 视图前缀 -->
                    <property name="prefix" value="/WEB-INF/templates/"/>
    
                    <!-- 视图后缀 -->
                    <property name="suffix" value=".html"/>
                    <property name="templateMode" value="HTML5"/>
                    <property name="characterEncoding" value="UTF-8" />
                </bean>
            </property>
        </bean>
    </property>
</bean>
  • 14
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
Thymeleaf - Spring Security integration modules [Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Status This is a thymeleaf extras module, not a part of the Thymeleaf core (and as such following its own versioning schema), but fully supported by the Thymeleaf team. This repository contains two projects: thymeleaf-extras-springsecurity3 for integration with Spring Security 3.x thymeleaf-extras-springsecurity4 for integration with Spring Security 4.x Current versions: Version 3.0.2.RELEASE - for Thymeleaf 3.0 (requires Thymeleaf 3.0.3+) Version 2.1.3.RELEASE - for Thymeleaf 2.1 (requires Thymeleaf 2.1.2+) License This software is licensed under the [Apache License 2.0] (http://www.apache.org/licenses/LICENSE-2.0.html). Requirements (3.0.x) Thymeleaf 3.0.0+ Spring Framework version 3.0.x to 4.3.x Spring Security version 3.0.x to 4.2.x Web environment (Spring Security integration cannot work offline) Maven info groupId: org.thymeleaf.extras artifactId: Spring Security 3 integration package: thymeleaf-extras-springsecurity3 Spring Security 4 integration package: thymeleaf-extras-springsecurity4 Distribution packages Distribution packages (binaries + sources + javadoc) can be downloaded from SourceForge. Features This module provides a new dialect called org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect or org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect (depending on the Spring Security version), with default prefix sec. It includes: New expression utility objects: #authentication representing the Spring Security authentication object (an object implementing the org.springframework.security.core.Authentication interface). #authorization: a expression utility object with methods for checking authorization based on expressions, URLs and Access Control Lists. New attributes: sec:authentication="prop" outputs a prop property of the authentication object, similar to the Spring Secu
配置Spring MVC中的Thymeleaf视图解析器,您可以创建一个Java类,并使用以下代码进行配置: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ServletContextTemplateResolver; @Configuration @EnableWebMvc public class ThymeleafConfig implements WebMvcConfigurer { @Bean public ViewResolver viewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); resolver.setCharacterEncoding("UTF-8"); return resolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolver(templateResolver()); return engine; } @Bean public ServletContextTemplateResolver templateResolver() { ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".html"); resolver.setTemplateMode("HTML"); return resolver; } } ``` 上述代码中,我们使用`@Configuration`注解将该类标记为配置类。`@EnableWebMvc`注解启用Spring MVC的基本配置。 `viewResolver()`方法创建一个Thymeleaf视图解析器,并设置了模板引擎为`templateEngine()`方法返回的SpringTemplateEngine。 `templateEngine()`方法创建一个SpringTemplateEngine,并设置了模板解析器为`templateResolver()`方法返回的ServletContextTemplateResolver。 `templateResolver()`方法创建一个ServletContextTemplateResolver,并设置了视图前缀、后缀和模板模式。 请注意,上述配置假设您的Thymeleaf模板文件位于`/WEB-INF/views/`目录下,并以`.html`作为文件扩展名。您可以根据您的实际项目结构和需求进行相应的更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oah1021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值