Spring Boot Request映射未加载静态资源 [英] Spring Boot Request mapping not loading static resources

查看:253
本文介绍了Spring Boot Request映射未加载静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring boot应用程序,在其中我提供一些存储在src/main/webapp目录中的静态jsp页面,其中包括一些存储在"src/main/resources/static"目录下的javascript参考.当我对"/somepath"执行请求映射时,它会成功返回在静态资源文件夹中具有javascript引用的视图,例如"src/main/resources/static/lib/script.js"

I have a spring boot application where I serve some static jsp pages stored in directory src/main/webapp which includes some javascript references stored under directory "src/main/resources/static". When I do the requestmapping for "/somepath" then it successfully returns view that has javascript references in static resources folder such as "src/main/resources/static/lib/script.js"

但是我现在面临的问题是当我对"/sompath/xyz"执行@requestmapping时,我收到了HTTP 404错误.当我检查页面源代码时,我发现页面正在此路径"/somepath/lib/script.js"中搜索静态文件夹,即"/somepath"正以默认资源位置作为前缀.有什么方法可以映射这些URI,以便无论那里有多少个"/path/",它都只会在"/static/"文件夹下查找资源,而不会像"/static/path"一样

But the problem that I'm facing right now is when I do the @requestmapping to "/sompath/xyz" then I get HTTP 404 error. When I inspected the page source then I found out that the page was searching for static folder in this path "/somepath/lib/script.js" that is "/somepath" is getting prefixed to the default resources location. Is there any way that I could map these URIs such that no matter how many "/path/" are there, it will look for resources under the "/static/" folder only and not like "/static/path"

感谢您的帮助.对于初学者,我为自己的问题结构深表歉意,但我仍在学习如何更好地理解所有这些问题.预先感谢.

Any help is appreciated. I apologise for my problem structure as I'm a beginner and I'm still learning to put all these things in better perspective. Thanks in advance.

推荐答案

我遇到了同样的问题,但发现了一半的解决方法.

I have same problem I found the way but its half solution.

第一

use RequestMapping path depth just one like 'spring'

在每种情况下都可以正常工作.

It is work well every situation.

use RequestMapping path depth tow like 'spring/xyz' and <link src="../some.css"/>

最后

use depth 3 'spring/xyz/123' and <link src="../../some.css"/>

这一切都弄乱了,但是可以正常工作.

This all is messed up but it is working.

所以我再次打开堆栈溢出问题

So I open the question on stack overflow again here please anyone come and check this and leave some idea thanks~!

这篇关于Spring Boot Request映射未加载静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆