SpringBoot中使用Thymeleaf模板引擎无法实现return转化怎么办???

我们知道在SpringBoot中可以在@Controller类下的 @RequestMapping("")下的方法中根据return可以跳转到resources/templates下的html页面。但是你这里要注意一点必须是使用@Controller的Controller类,**绝对不可以使用@RestController,使用@RestController是无效的。 **
1 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zxf</groupId>
    <artifactId>spring_boot_0828</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_boot_0828</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!--
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.3.3</thymeleaf-layout-dialect.version>
        -->
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
              <!--模板引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2 Cotroller层处理类

package com.zxf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
//@RestController //它是无效的。不支持模板引擎
@Controller  //第一要使用它配置该类
@RequestMapping("my1")
public class MyController1 {
    @RequestMapping("hello")
    public String hello(ModelMap modelMap){
          modelMap.addAttribute("name","zhang6132326");
        return "list";
    }
}

3 Html文件位置及代码
SpringBoot中使用Thymeleaf模板引擎无法实现return转化怎么办???_第1张图片

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>list页面</title>
</head>
<body bgcolor="#ffa243">
<h1 th:text="${name}" ></h1>
<p>aaabbcc</p>
</body>
</html>

application.properties属性配置

server.servlet.context-path=/boot
#thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.charset=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html

你可能感兴趣的:(SpringBoot)