0

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

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

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

I found half solution here

my folder structure is:

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

case 1: works well

java:
    @RequestMapping("spring")

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

case2: works well

java:
    @RequestMapping("spring/xyz")

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

case3: not working

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

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

it is called 'http//localhost/spring/xyz/css/some.css'

case3: works well

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

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

case4: works well

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.

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

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~!!

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

4
  • Just use slash at the beginning of the address: <link rel="stylesheet" href="/spring/xyz/css/some.css"> Commented Nov 29, 2016 at 12:44
  • wow it works well. I want know how to work this behind. can you give some spring doc or explain it? Thanks @NickSavenia Commented Nov 29, 2016 at 13:01
  • stackoverflow.com/a/39545949/6066470 Commented Nov 29, 2016 at 13:17
  • @NickSavenia really helpful !!! Commented Nov 29, 2016 at 13:23

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.