4 如何配置springboot跳转html页面(thymeleaf)

注意: jss .css默认去static文件中取找

            html默认去templates中取去找 

1、首先在pom.xml添加对HTML的相关依赖

/**
 * pom.xml文件
 */
<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>
    </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>**


2、在application.yml文件添加SpringBoot相关配置

spring:
  thymeleaf:
    prefix: classpath:/templates/



3、创建HTML文件

/**
 * 路径:resources/templates
 */
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>第一个HTML页面</title>
</head>
<body>
<h1>Hello Spring Boot!!!</h1>
<p th:text="${hello}"></p>
</body>
</html>

  

4、在Controller里面写跳转HTML页面方法

@Controller  //注意这里必须为Controller
public class HelloController {

    /**
     * 本地访问内容地址 :http://localhost:8080/lmycc/hello
     * @param map
     * @return
     */
    @RequestMapping("/hello")
    public String helloHtml(HashMap<String, Object> map) {
        map.put("hello", "欢迎进入HTML页面");
        return "/index";
    }
}

加载类:

package lmycc.test.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}


5、访问页面 

 https://blog.csdn.net/sinat_33889619/article/details/78339042 

猜你喜欢

转载自blog.csdn.net/xiaoxiaoniaoQ/article/details/84562285