static和templates 是springboot默认创建的文件夹,static中放静态页面,而templates中放动态页面
存放在static目录下,浏览器可以直接访问 http://localhost:8080/index.html,图片资源以及css,js可以放在此目录下,而templates 不能访问
若整合thymeleaf,需要引入jar
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
编写controller进行跳转
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
@Controller
public class IndexController {
/*
本地访问内容地址 :http://localhost:8080/hello
*/
@RequestMapping("/hello")
public String helloHtml(HashMap map) {
map.put("hello", "欢迎进入HTML页面");
return "/index";
}
/*
当引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转
想跳转到/static/index.html,可以使用重定向return "redirect:/index.html"
*/
@RequestMapping("/hello2")
public String hello2Html() {
return "redirect:/index.html";
}
}
动态的html代码
templates中放动态页面templates中放动态页面
静态html代码
static中放静态页面static中放静态页面
效果图如下
若在static下创建文件夹如css,js,images,必须在application.yml文件中进行声明,即使是默认的配置,否者无法访问
mvc:
static-path-pattern: /**