0

I have a simple question. In a Spring Boot Application I have a controller that works fine:

@GetMapping("/mycats")
public String getCats(){
    return "cats.html";
}

cats.html is a html file in resources/static/

When I change the action URL like this

@GetMapping("/mycats/my")
public String getCats(){
    return "cats.html";
}

Spring cannot find the html file anymore. I have tried many directory combinations and still no success. I don't use thymeleaf/jsp. Why is that happening?

2
  • I guest that is not good approach if you are using cats.html and you are not using jsp,jsf,etc.. then you can communicate via ajax. Don't use post-back operations Commented Mar 6, 2017 at 12:57
  • Single Page Applications do not use post-back operations Commented Mar 6, 2017 at 12:59

1 Answer 1

1

This is due to the context. When you use "mycats" it will look for the page in static directory. but when you use "mycats/my" it will look for the page in the static/my directory. This directory does not exists, so you get a 404 error.

You can make a little change to you controller. You can command it that look for in the previos directory with "../", but you always have to be only on directory deep.

@GetMapping("/mycats/my")
public String getCats(){
    return "../cats.html";
}

Or from any directory, you can tell spring that looks at root directory with "/"

@GetMapping("/mycats/my")
public String getCats(){
    return "/cats.html";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the late reply. It is like you say. The thing that confused me is that both routes work with thymeleaf and jsp. Since html is a static resource it makes sense to work like this. Thank you!

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.