springboot访问静态资源方法
这是将vue项目放到静态包下 根目录
端口和前缀
vue.html
<!DOCTYPE html> <!--suppress ALL --> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>vxe Demo</title> <meta name="description" content="Element UI Demo"> <link rel="stylesheet" th:href="@{/assets/persional/views/dist/css/app.6aa77349.css}"/> <link rel="stylesheet" th:href="@{/assets/persional/views/dist/css/chunk-vendors.59514fc7.css}"/> <p>fdsfdsfdsgfdsgfdghfdshfdgfdgdf</p> </head> <body> <script defer="defer" type="module" th:src="@{/assets/persional/views/dist/js/chunk-vendors.42ad1716.js}"></script> <script defer="defer" type="module" th:src="@{/assets/persional/views/dist/js/app.105eb1f3.js}"></script> <script defer="defer" th:src="@{/assets/persional/views/dist/js/chunk-vendors-legacy.c0641cf1.js}" nomodule></script> <script defer="defer" th:src="@{/assets/persional/views/dist/js/app-legacy.b0780305.js}" nomodule></script> <div id="app"></div> </body> </html>
方法一
yml中配置如下
spring: mvc: static-path-pattern: /sta/** #配置静态资源的访问前缀:改了之后想要直接加载静态资源要用http://localhost:端口号/sta/静态资源名。 这个sta是虚拟的,并不是真实存在的路径,这样改之后可以防止与Controller中的访问冲突 #resources: #static-locations: [classpath:/haha/] #这是改变静态资源的存放路径 #add-mappings: true #为true表示可以用上面提到的规则直接访问静态资源,false时表示禁止所有静态资源规则
成功访问http://localhost:58080/seckill/sta/templates/model/views/vue.html
方法二
承接方法一,写一个ModelAndView
@RestController @RequestMapping("/view") public class IndexView { @GetMapping("/index") public ModelAndView login() { return new ModelAndView("/sta/templates/model/views/vue.html"); } }
访问路径http://localhost:58080/seckill/view/index 成功了
虽然上述方案能够链接了html,但是没能引进css和js
方法三
直接访问
@Controller @RequestMapping("/view") public class IndexView { @GetMapping("/index") public String login() { return "/templates/model/views/vue.html"; } }
若是配置了mvc
mvc: static-path-pattern: /sta/** #配置静态资源的访问前缀:改了之后想要直接加载静态资源要用http://localhost:端口号/sta/静态资源名。 这个ers是虚拟的,并不是真实存在的路径,这样改之后可以防止与Controller中的访问冲突
则
@Controller @RequestMapping("/view") public class IndexView { @GetMapping("/index") public String login() { return "/sta/templates/model/views/vue.html"; } }
成功 但是,如果加了依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
并且配置了
#thymeleaf: #suffix: .html # 过滤所有非html结尾的文件 #classpath: /templates/model/** #则不指定suffix: .html --> 扫描/templates/model/下所有文件 #prefix: classpath:/templates/model/ # 扫描/templates/model/下所有文件 #cache: false # 关闭缓存 开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。 #encoding: UTF-8 #check-template-location: true #check-tempate-location: 检查模板路径是否存
将会报错 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Apr 29 11:18:36 CST 2022 There was an unexpected error (type=Internal Server Error, status=500). Error resolving template [/sta/templates/model/views/vue.html], template might not exist or might not be accessible by any of the configured Template Resolvers
上一篇:
IDEA上Java项目控制台中文乱码