作为SpringBoot初学者,我想知道SpringBoot怎么访问templates下的html页面。搜了一下,各有各的说法。自己动手,不是这儿出问题就是那出问题。把自己的经验写在这里。如有问题,多交流,评论即可……
项目结构:
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.properties
spring.thymeleaf.prefix=classpath:/templates/
html1: http://localhost:8080/html/hello1.html
<html>
<head>
<title>hello1</title>
</head>
<body>
<p>hello1</p>
<p>访问方式:http://localhost:8080/html/hello1.html</p>
</body>
</html>
html2:http://localhost:8080/hello2.html
<html>
<head>
<title>hello2</title>
</head>
<body>
<p>hello2</p>
<p>访问方式:http://localhost:8080/hello2.html</p>
</body>
</html>
html3:http://localhost:8080/html
<html>
<meta charset="utf-8"></meta>
<head>
<title>hello3</title>
</head>
<body>
<p>hello3</p>
<p>访问方式:http://localhost:8080/html</p>
</body>
</html>
HtmlController:(此处用的是@Controller)
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HtmlController
{
@RequestMapping("/html")
public String getHtml() {
System.out.println("hello3");
return "hello3";
}
}