关于SpringBoot页面跳转以及访问静态资源问题总结

 

在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="${request.contextPath}/statics/index.html">资源统计分析</a>

 


 

posted @ 2021-04-29 16:29  万里哥  阅读(2330)  评论(0编辑  收藏  举报