SpringBoot中配置Web静态资源路径的⽅法
介绍:本⽂章主要针对web项⽬中的两个问题进⾏详细解析介绍:1- 页⾯跳转404,即controller转发⽆法跳转页⾯问题;2- 静态资源⽂件路径问题。项⽬⼯具: Intelij Idea, JDK1.8, SpringBoot 2.1.3
正⽂:
准备⼯作:通过Idea创建⼀个SpringBoot-web项⽬,此过程不做赘述,创建完成后项⽬结构如下图:
1- 创建⼀个controller代码如下:
ller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("demo")
public String demo() {
System.out.println("进⼊controller中的demo⽅法!");
/*注意:这⾥返回值有后缀名,如何省略后缀名后⾯有介绍*/
return "myPage.html";
}
}
2- 在 web-practice\src\main\resources\templates\路径下创建html页⾯,取名“myPage”,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to myPage!</h1>
</body>
</html>
此时运⾏项⽬,会发现报404问题,同时查看Idea控制台,打印显⽰进⼊controller⽅法。
sources.static-location登场
打开l⽂件,进⾏如下配置(默认项⽬中配置⽂件为application.properties,修改后缀名即可,因我个⼈喜欢使⽤yml⽂件),重新运⾏项⽬并访问地址:localhost:8080/demo 会发现页⾯跳转成功。
spring:
resources:
static-locations: classpath:templates/
原因分析:sources.static-location参数指定了Spring Boot-web项⽬中静态⽂件存放地址,该参数默认设置为:
classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,servlet context:/,可以发现这些地址中并没有/templates这个地址。当配置⽂件中配置此项后,默认配置失效,使⽤⾃定义设置。这⾥涉及到两个概念:
(1)classpath:  通俗来讲classpath对应的项⽬中:web-practice\src\main\resources ⽂件⽬录。如:“classpath: templates/” 即是将resources⽬录下的templates⽂件夹设置为静态⽂件⽬录。更深⼀步讲classpath路径为:⽂件编译后在target/classes⽬录下的⽂件。
(2)静态⽂件⽬录:通俗理解为存放包括:.html;.jsp;CSS;js;图⽚;⽂本⽂件等类型⽂件的⽬录。这些⽂件都可以通过浏览器url进⾏访问。同时controller中转发的⽂件⽬录也必须被设置为静态⽂件⽬录,这就是增加了该参数以后就可以正常访问的原因。
4-  spring.mvc.view.prefix/suffix登场
现在页⾯已经可以正常转发,我们有了新的想法,我希望在templates⽂件夹中创建⼀个html⽂件夹⽤
于专门存放页⾯⽂件,另外在每次使⽤controller进⾏转发是都要标明后缀名.html,这很⿇烦,有没有统⼀处理的⽅案,答案当然是有!
修改后项⽬结构如下:
controller⽅法修改如下:
ller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("demo")
public String demo() {
System.out.println("进⼊controller中的demo⽅法!");
//如果不在l⽂件中添加前后缀信息,此处返回语句为
//return "html/myPage.html"
return "myPage";
}
}
spring:
resources:
static-locations: classpath:templates/
mvc:
view:
prefix: html/
suffix: .html
再次运⾏项⽬即可。通过测试得知prefix/suffix是在controller返回语句前后添加前后缀信息。
5- 配置多个静态⽂件路径:当我们在页⾯中添加图⽚,并且将图⽚存放在resources/static/pic路径下,如下图所⽰:springboot结构
修改myPage.html如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to myPage!</h1>
<img src="/pic/pig.jpg" height="1920" width="1080"/></body>
</html>
之后重启项⽬,会发现图⽚并没有成功加载!如下:
原因是之前我们配置的静态⽂件⽬录只包含classpath:templates/,static⽬录还不是合法的存储静态⽂件⽬录,我们只需要在后⾯追加上static⽬录即可。修改l⽂件如下:
spring:
resources:
static-locations: classpath:templates/,classpath:static/
mvc:
view:
prefix: html/
suffix: .html
修改后重启项⽬刷新页⾯,⼀切正常!
6- 关于spring.mvc.view.static-path-pattern
该参数⽤来规定访问静态⽂件的路径格式,该参数默认值为:“/**” 表⽰所有路径,现将该参数修改为:“/static/**” 观察现象
spring:
resources:
static-locations: classpath:templates/,classpath:static/
mvc:
view:
prefix: html/
suffix: .html
static-path-pattern: /static/**
重启项⽬,发现页⾯不能加载404错误!
要解决该问题需要修改两个地⽅:
(1)修改spring.mvc.view.prefix参数值为:static/html/  ;该修改为了controller转发时可以到⽂件路径;
(2)修改myPage页⾯的图⽚地址如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to myPage!</h1>
<img src="/static/pic/pig.jpg" height="1920" width="1080"/></body>
</html>
原因分析:static-path-pattern规定的时访问静态页⾯的路径类型,这⾥规定访问静态页⾯必须为:localhost:8080/static/***的⽅式才能访问到静态资源。static-path-pattern 并不是规定实际的静态⽂件访问路径,⽽是规定了⼀种url标记,只有遵循该标记的规则才能访问静态⽂件。
扩展:
sources.static-locations参数除了规定classpath:路径下的⽂件⽬录为静态⽂件⽬录,还可以规定项⽬以外的位置,如设置:E:/test⽂件夹⽬录为静态⽂件存储⽬录,如下:
spring:
resources:
static-locations: classpath:templates/,classpath:static/,file:E:/test
2- 页⾯访问过程如下:
浏览器发送请求,先匹配SpringMVC中RequestMapping列表,匹配到后根据controller返回值定位静态资源⽬录,并返回给客户;如果RequestMapping中未匹配到,则判断是不是静态⽂件⽬录,如果是的话直接到静态⽂件⽬录对应路径下查询⽂件,查询到返回,未查询到不返回。
3- static-location配置的⽬录列表都被视为根⽬录,如果两个⽬录中相同⽂件⽬录下存储了同名同类型⽂件,返回在static-locations配置靠前的根⽬录下的内容。
4- static-path-pattern参数规定了静态⽂件存储路径,在controller的RequestMapping中应该避免设置该路径相同的访问路径。
到此这篇关于SpringBoot中配置Web静态资源路径的⽅法的⽂章就介绍到这了,更多相关SpringBoot配置Web静态资源路径内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。