I have to do a Spring Boot project for Uni and I have chosen the most basic (in my opinion) technologies for frontend, those being HTML and JQuery. I have a @Controller
which returns the html files.
@Controller
public class HomeController {
@GetMapping("/a/b")
public String ab(){
System.out.println("Hello");
return "page.html";
}
}
The page page.html
is placed correctly in the /resources/static/
folder.
The issue is here: When I access the /a/b
URL, the DispatcherServlet forwards /a/b
to /a/page.html
, which it says it cannot find. Here are the logs:
Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7]
View name 'page.html', model {}
Forwarding to [page.html]
"FORWARD" dispatch for GET "/a/page.html", parameters={}
Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
: Resource not found
: Resolved [org.springframework.web.servlet.resource.NoResourceFoundException: No static resource a/page.html.]
Exiting from "FORWARD" dispatch, status 404
Completed 404 NOT_FOUND
"ERROR" dispatch for GET "/error" parameters={}
Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
The strange thing is that if I change the URL from /a/b/
to only one path segment /a
, it works fine and displays the html page.
What am I not understanding or is there a way to configure the dispatcher to stop forwarding to an URL generated by replacing the last path segment with the view name?
Adding
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
to application.properties
"fixed" it for me (I still do not understand why that forwarding was happening).
Also, when importing css and js into your html file, specify them with a /
at the beginning, like this href="/css/base.css"
.