springboot整合thymeleaf

  1. thymeleaf是对html进行渲染的一种技术。
  2. src/main/resources/templates是目录安全的,不能通过外界直接访问。
  • 创建springboot项目,修改pom.xml文件
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.8.RELEASE</version>
    	</parent>
    	<groupId>com.test</groupId>
    	<artifactId>spring-boot-freemarker-04</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<!-- 修改jdk的版本 -->
    	<properties>
    		<java.version>1.8</java.version>
    		
    	</properties>
    	<!-- springboot启动类坐标 -->
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- 添加thymeleaf的启动类 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    	</dependencies>
    </project>

     

  • 创建controller

    package com.test.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * thymeleaf的入门案列
     * @author 26920
     *
     */
    @Controller
    public class DemoController {
    	@RequestMapping("/hello")
    	public String hello(Model model){
    		model.addAttribute("msg", "hello world");
    		return "index";
    	}
    }
    

     

  • 创建html视图

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<span th:text="hello"></span>
    	<hr/>
    	<span th:text="${msg}"></span>
    </body>
    </html>

     

  • 创建启动类,启动项目,进行访问

  • 直接访问会有一个异常

     

  • 解决方案是:Thymeleaf.jar:更新为 3.0 以上的版本, thymeleaf-layout-dialect.jar:更新为 2.0 以上的版本,修改pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.8.RELEASE</version>
    	</parent>
    	<groupId>com.test</groupId>
    	<artifactId>spring-boot-freemarker-04</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<!-- 修改jdk的版本 -->
    	<properties>
    		<java.version>1.8</java.version>
    		<!--修改thymeleaf的版本 -->
    		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
    	</properties>
    	<!-- springboot启动类坐标 -->
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- 添加thymeleaf的启动类 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    	</dependencies>
    </project>

    添加了thymeleaf的版本控制

  • 启动项目访问就可以看到页面效果了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值