4 如何配置springboot跳轉html頁面(thymeleaf)
阿新 • • 發佈:2018-12-03
注意: 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