javacssjspspring-mvcweb-inf

reference a css file in deployed webapp


I have deployed a webapp at localhost:8080/Project with Project being the context name. The webapp correctly redirects the user to the index.jsp.

But the css file in this index.jsp is not found (Error 404).

Index.jsp contains this reference:

<html lang="en">
<head>
    <link href="../css/style.css" rel="stylesheet">
</head>
</html>

My project has the following structure in the WEB-INF folder:

WEB-INF/views/index.jsp
WEB-INF/css/style.css

I also tried to directly call the css file via url localhost:8080/Project/css/style.css but this didn't work as well.


I relocated the css folder one level up (on the same level as WEB-INF) as suggested. The problem still persists. Do I have to exclude the urls starting with /css in this @Controller? Or maybe I'm on a wrong path...

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        return "index";
    }
}

Solution

  • The browser loaded localhost:8080/Project and then continues to load ../css/style.css relative to that URL, therefore trying to load localhost:8080/css/style.css which apparently does not work.

    You should not place web resources like CSS, JS and images under /WEB-INF since they cannot be accessed from that location. Instead use something like

    WEB-INF/views/index.jsp
    css/style.css
    

    and in the JSP write

    <link href="css/style.css" rel="stylesheet">