springboot2.x整合thymeleaf的404(找不到资源)500(html名和访问地址一样)问题


参考文档: https://www.cnblogs.com/yichunguo/p/12128324.html

引入问题

父版本号

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

版本号 2.0.7-2.1.3 出现 java.lang.ClassNotFoundException: org.attoparser.config.ParseConfiguration

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

版本号 2.1.4以上出现 javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
或者
There was an unexpected error (type=Not Found, status=404).
No message availabl
网上说500解决办法
1.消除缺省转发
2.修改view和path,让他们不同名(换成不同名报404

我的办法是降版本降到2.0.7以下


可用版本号

<?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.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.common</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>
    <description>author:klz</description>

<!--    版本控制-->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--        <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>-->
<!--        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>-->
    </properties>

    <dependencies>

<!--        web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!--        热部署依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

<!--        lombok插件依赖-->
       <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

<!--        springboot测试依赖-->
        <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>

 <!--        thymeleaf模板依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.thymeleaf</groupId>-->
<!--            <artifactId>thymeleaf-spring5</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.thymeleaf.extras</groupId>-->
<!--            <artifactId>thymeleaf-extras-java8time</artifactId>-->
<!--        </dependency>-->


    </dependencies>

    <build>
        <plugins>

<!--            打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>
</project>

跳转方式(Spring MVC或Spring Boot配置默认访问页面不生效?)

参考文档: https://www.cnblogs.com/geshanzsq/p/11089169.html

我这里的两种写法都可以

controller

@Controller
public class IndexController {
   @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }
}

WebMvcConfig

@Configuration
//@EnableWebMvc
public class MyWebmvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
    }
}

application.yml配置

ThymeleafPropeties这个类中有默认的配置,springboot默认装配了这些配置,这个地方application.yml默认的配置可以不写,可以写一些自定义的配置(我这个地方是默认配置,所以可以删掉)

ThymeLeaf配置

spring:
  thymeleaf:
    #模板的模式,支持 HTML, XML TEXT JAVASCRIPT
    mode: HTML5
    #编码 可不用配置
    encoding: UTF-8
    #内容类别,可不用配置
    #开发配置为false,避免修改模板还要重启服务器
    cache: false
    #配置模板路径,默认是templates,可以不用配置
    prefix: classpath:/templates/
    servlet:
      content-type: text/html

命名空间引入

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
    <p3>index</p3>
    <p>this page is index </p>
</body>
</html>

有的地方不知道为什么,欢迎评论指正…