Springboot如何访问resource下html静态页面
1. 先把我的项目结构包发出来看
2. 首先写好html页面
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
这个html是顺手在spring官网复制的,用的thymeleaf模板先引入,所以需要导入依赖。
3. pom.xml中导入thymeleaf依赖
spring-boot-starter-test还有spring-boot-starter-web是我们创建springboot模板的时候自动加的依赖,
这时候我们应该在里面跟一段依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4. 在application.properties为文件中配置文件路径
spring.thymeleaf.prefix=classpath:/templates/
这一行是标明模板地址的前缀,找页面的时候会先加载templates下的子目录
写上这么一行后就可以进行访问了
5. Controller访问路径
package com.example.boke.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam(name="name") String name,Model model){
model.addAttribute("name",name);
return "hello";
}
}
_我的映射路径是_http://localhost:8087/helloname=%E9%99%88%E5%B7%A7%E5%87%A4
因为我用了注解传name的值所以才写了后面的名字,进来找bug的人如没写传值,此地可省略,然后我成功的页面
最后总结,遇见bug不要慌张,按步骤先检查代码,再检查环境,最后度娘。