1.將前端的vue項目生成的文件放到后端對應目錄
前端文件:
后端目錄結構:
2.引入thymeleaf依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.更改配置文件
# thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.cache=false spring.resources.static-locations=classpath:/static/ spring.mvc.static-path-pattern=/static/**
4.添加代碼
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("") public class IndexController { @RequestMapping("/index") public String index(){ return "index"; } }
5.瀏覽器打開
訪問地址為:
http://localhost:8080/index
6.補充
如果想配置多個spring.resources.static-locations與spring.mvc.static-path-pattern
@Configuration public class MyWebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/"); registry.addResourceHandler("/common/**").addResourceLocations("classpath:/common/"); }
}