spring学习笔记(十六)springboot_web开发

4.3 引入Thymeleaf模板引擎

模板引擎:JSP、Velocity、Freemarker、Thymeleaf

SpringBoot推荐的Thymeleaf;

语法更简单,功能更强大;

引入thymeleaf;

 

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<properties>
     <java.version>1.8</java.version>
     <!-- 切换thymeleaf版本 -->
     <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
     <!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
     <!-- thymeleaf2   layout1-->
     <thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version></properties>

4.4 thymeleaf语法

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

	private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

	public static final String DEFAULT_PREFIX = "classpath:/templates/";

	public static final String DEFAULT_SUFFIX = ".html";

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

使用:

1、导入thymeleaf的名称空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!-- 导入后该语法会有提示信息 -->

2、使用thymeleaf语法

controller:

@Controller
public class TestContorller {

    @RequestMapping("/success")
    public String success( Map<String,Object> map){
        map.put("hello", "hello world");
        return "success";
    }
}

html: 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>成功!</h1>
    <!--th:text 将div里面的文本内容设置为我们指定的值 -->
    <div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

启动SpringBoot,访问http://localhost:8081/success

问题:

配置thymeleaf之后跳转HTML页面不成功的问题?

return " success";这里多写了一个空格,导致找不到与之匹配的html页面,

注意:controller里return的值应该是html页面的名字,如我的html页面是“success.html",则return "success"即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值