How to reference a .css file from a view (.jsp) in

2019-05-18 20:13发布

问题:

This is the structure: (they're in the same directory!)

Directory
|-view.jsp
|-stylesheet.css

When I do <link href="stylesheet.css" rel="stylesheet" media="screen"> The .css file does not get referenced correctly, i.e. I have no idea what path to set to get to it (if put as a URL in the browser, I get a 404).

I guess it gets translated as http://localhost:8080/myApp/stylesheet.css and then there is no mapping defined for it. Logging says:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myApp/stylesheet.css] in DispatcherServlet with name 'appServlet'

It should work like this, shouldn't it? For example, this works:

<%@ include file="include.jsp"%>

include.jsp is in the same folder as well.

回答1:

JSP views and other resources stored under WEB-INF/ are not directly accessible to the end-user, they are web application's private resources and the server doesn't expose them. You'll have to place any public resources one level above WEB-INF/, for example:

webapp/
|-- style/
|     stylesheet.css
|-- images/
|     image1.png
|     image2.png
|-- html/
|     index.html
+- WEB-INF/
  +-- jsp/
        view.jsp
        include.jsp