Reputation: 37098
I develop web application.
Spring+Hibernate on Tomcat servlet container.
Today on another PC I deploy application and see that css doesn't load.
in jsp I use relative paths for this(example)
<link href="/resources/css/ui-lightness/jquery-ui-1.10.0.custom.min.css" rel="stylesheet">
Respective request which browser sends:
http://localhost:8080/resources/css/ui-lightness/jquery-ui-1.10.0.custom.min.css
and this request returns 404 http error.
for another jsp:
<link href="/resources/css/bootstrap.min.css" rel="stylesheet">
browser sends:
http://localhost:8080/terminal-company/resources/css/bootstrap.min.css
Thus you can see that from first jsp project name doesn't add to the URL
Why? and how to fix it? request me detail which I should to add to relevant answer.
spring related part of web.xml:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/webContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Upvotes: 1
Views: 1177
Reputation: 692023
If your goal is to be able to use absolute paths, without caring (and knowing) about the context path of the webapp (/terminal-company
in your second example), then use the JSTL <c:url>
tag to generate all your URLs:
<link href="<c:url value='/resources/css/bootstrap.min.css'/>" rel="stylesheet">
The second example will send a request to /resources/css/bootstrap.min.css
, and not to /terminal-company/resources/css/bootstrap.min.css
, unless there is a <base>
tag in the generated HTML page.
EDIT: your original question didn't say you were using spring, and mapped / to the dispatcher servlet. Spring is thus in charge of service resources. The documentation explains how to configure it. But that doesn't change anything to the above answer: to be independant of the context path, use c:url.
Upvotes: 2
Reputation: 745
I have experienced the same behaviour running on localhost. Just add the default servlet which will then serve static resources like css, js, images and so on. The mapping must be placed before your "/" urlpattern because "/" will match "/js".
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/css/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
Upvotes: 0
Reputation: 233
you have forgot to add servlet mapping in web.xml for your css files.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
This applies same for all your js, png.
This will work i guess cause i also had same issues.
Upvotes: 0