springBoot(1)thymeleaf使用和扩展springmvc

thymeleaf使用和语法

1. 引入thymeleaf

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

2. thymeleaf使用和语法

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    //默认的前缀
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    //默认的后缀
    public static final String DEFAULT_SUFFIX = ".html";
    //只要把html页面放在classpath:/templates/,thymeleaf就会自动渲染

只要把html页面放在classpath:/templates/,thymeleaf就会自动渲染

使用:

  1. 导入thymeleaf的名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  1. 使用thymeleaf语法
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>成功页面</h2>
    <!--th:text="" 将div里面的内容设置为-->
    <div th:text="${hello}"></div>
</body>
</html>

扩展SpringMVC

编写一个配置类(@Configuration),是继承WebMvcConfigurer借口的(在SpringBoot 2之后的版本改用WebMvcConfigurer这个接口来注册controllers);不能标注@EnableWebMvc。

//使用WebMvcConfigurer用来拓展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送 /wuhu 来到 success页面
        registry.addViewController("/wuhu").setViewName("success");
    }
}

使用@EnableWebMvc将会全面接管SpringMVC配置,自动配置将失效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值