Spring Boot不会加载静态资源,这取决于RequestMapping深度 [英] Spring Boot not loading static resources it depends on RequestMapping depth

查看:163
本文介绍了Spring Boot不会加载静态资源,这取决于RequestMapping深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Spring Boot应用程序的静态文件夹下加载文件.

I have problem to load the file under static folder on spring boot application.

问题是RequestMapping深度大于2,例如@RequestMapping("spring/xyz")

The problem is RequestMapping depth more than 2 like @RequestMapping("spring/xyz")

@RequestMapping("spring")单一深度效果很好,但是2深度以'spring'开头,它连接localhost:8080/spring/'static folder'

The @RequestMapping("spring") single depth works well but 2 depth is prefixed 'spring' it is connect localhost:8080/spring/'static folder'

我在此处

我的文件夹结构是:

static/css/some.css  
static/templates/velocity.vm

情况1:效果很好

java:
    @RequestMapping("spring")

html:
    <link rel="stylesheet" href="css/some.css">

情况2:效果很好

java:
    @RequestMapping("spring/xyz")

html:
    <link rel="stylesheet" href="../css/some.css">

案例3:不起作用

case3: not working

java:
    @RequestMapping("spring/xyz/123")

html:
    <link rel="stylesheet" href="../css/some.css">

它称为'http//localhost/spring/xyz/css/some.css'

情况3:效果很好

java:
    @RequestMapping("spring/xyz/123")

html:
    <link rel="stylesheet" href="../../css/some.css">

情况4:效果很好

java:
    @RequestMapping("123")

html:
    <link rel="stylesheet" href="../../css/some.css">

有效!!即使我使用../../相对路径. 我不知道为什么会这样.

It works!! even if I use ../../ relative path. I don't know why this works.

实际上,我不太了解Spring Boot API,因为我认为使用ViewResoler会加载其他静态资源.

Actually I didn't understand Spring Boot API well that I consider use ViewResoler something load other static resources.

我想知道这种加载路径的机制,以及如何通过RequestMapping URL路径链接来调用"http//localhost/spring/xyz/css/some.css"

I want to know this load path machanism and how to the RequestMapping url path link to call the 'http//localhost/spring/xyz/css/some.css'

我请任何答案谢谢〜!!

I appriciate any answer thanks~!!

我在spring.io上提到了同一问题

I refer to the same issue on spring.io here from 'metalhead' and 'Brian Clozel'

推荐答案

如果您使用百里香叶,还可以将路径指定为:

if you are using thymeleaf you can also specify the path as :

<link rel="stylesheet" th:href="@{/css/main.css}"
href="../static/css/main.css" />

对于我来说,无论深度如何,它都正常工作.

it worked properly for me for any depth.

这篇关于Spring Boot不会加载静态资源,这取决于RequestMapping深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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