关于SpringBoot页⾯跳转以及访问静态资源问题总结
springboot原理pdf在springboot项⽬中默认访问路径是static⽂件夹和template⽂件夹,static⽂件夹下的资源可以通过浏览器直接访问(如:
localhost:8080/index.html,如有⽂件夹输⼊正确路径即可如:localhost:8080/image/abc.jpg),⽽template⽂件夹下的页⾯需要在controller中跳转进⾏访问。在controller中如果直接return页⾯的名称那么默认从template⽂件夹中去寻对应的页⾯,例如:
@RequestMapping("/login")
public String login() {
return "/login";
}
这样的情况会默认去template⽂件夹下寻login.html去跳转。
那么如果html⽂件在static⽂件夹下就需要这么写来进⾏跳转:
@RequestMapping("/login")
public String toIndex() {
return "../static/login.html";
}
假如你js这么写,这个代码的意思是跳转到login这个请求,SpringBoot会根据Controller中的RequestMapping到"/login"请求,然后执⾏⾥⾯的内容,并不是跳转到"signin.html"
window.location.href = "login";
假如你js这么写,也会失败,因为服务器会把"signin.html"当成⼀个请求,原理跟上⾯第⼀点⼀样。
window.location.href = "signin.html";
如果需要直接超链接跳转到页⾯,⽽不经过请求,那就把要跳转的页⾯放在static下,没有这个⽂件夹就新建就好。然后别⽤这个window.location.href写,
直接⽤超链接
<a href="${tPath}/statics/index.html">资源统计分析</a>

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