1

I have a problem with handling css files in Spring MVC. Something is wrong with the location and mapping of the css file.

If the css file is in:

-src
  -main
     +java
     +resources
     -webapp
        -css
           style.css
        +WEB-INF

(Maven project)

I use:

 <mvc:resources location="/css/" mapping="/css/**"/>
 <mvc:annotation-driven/> 

in dispatcher-servlet.xml, and access access it in the jsp with:

<head>
  <title>Insert title here</title>
  <link href="/css/style.css" rel="stylesheet" type="text/css">
</head>
4
  • 1
    Hi Evgeny, please show us what the error messages you're getting are when you try to go to localhost:8080/css/style.css in your browser, as well as what stacktraces you see in the logs. NOTE localhost:8080 in the address bar should be replaced with whatever servername you use to access your project, if that's not already clear. Also, it would help to see the servlet configuration from web.xml. Use the edit link to edit your question with these details. Commented May 26, 2012 at 19:26
  • Hi, thanks for the response. localhost:8081/table/css/style.css shows me the content of the css file. Chrome's development tools tells me: "GET localhost:8081/css/style.css 404(Not found)"(No my project name here...), in the jsp. Commented May 26, 2012 at 19:39
  • So table is your project name? Commented May 26, 2012 at 19:55
  • 1
    So if you change <link href="/css/style.css" rel="stylesheet" type="text/css"> to <link href="/table/css/style.css" rel="stylesheet" type="text/css"> does it work? Context paths off of localhost always were confusing... Java drops "table" from the uri patterns, but the browser sees it as part of the path... Commented May 26, 2012 at 20:04

2 Answers 2

5

Using JSTL c tag to solve the context path problem:

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 ...
 <link rel="stylesheet" type="text/css" href="<c:url value='/css/style.css'/>"/>

which will give you:

 <link rel="stylesheet" type="text/css" href="/{context-path}/css/style.css"/>

This tag is recommended and you should wrap all your urls with it since the JSLT processor will generate the correct context path for you even if you decide to change the context path later without having to go through every single link in all jsp files.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi gigadot, I'm out of votes, but you'll definitely get my +1 later. I think the best approach is to use both the base tag in combination with the c:url tag. OP still gets the benefits you mention if the context path changes, but only has to change just the base element instead of every single link on the project. Great answer :)
3

To solve this problem so that your code will run with the actual paths you would use in production, use a <base> tag in your JSP pages, like so:

<base href="http://localhost:8081/table/" target="_blank" />

This will ensure that all of the rest of the relative paths in your JSP page can be represented using the full paths:

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

For further reading, here is a reference at the Mozilla Developer Center on the usage of the HTML base element. Additionally, this blog post may also be helpful in giving you a perspective on working with client-side code, especially if you work with Web designers who are not Java developers.


NOTE: As an aside, if you combine @gigadot's proposed solution to use c:url tags with the base tag, then it will save you the trouble of needing to edit every single link on your site and cluttering the HTML, while still making the base tag be dynamic so that if you change the context path, it will still be reflected everywhere, but in a manner that doesn't bind you so tightly to the framework.

I'm linking to that answer so that the credit for using the <c:url tag goes to @gigadot, yet documenting it here so that others can benefit from the combined knowledge of using both together.

<base href="<c:url value='/table' />" target="blank" />

3 Comments

Hi Evgeny, as an aside, if you combine gigadot's proposed solution with the base tag, then it will save you the trouble of needing to edit every single link on your site and cluttering the HTML, while still making the base tag be dynamic so that if you change the context path, it will still be reflected everywhere, but in a manner that doesn't bind you so tightly to the framework. :)
i don't think you want to have target="_blank". that will make all links to be opened in new windows, right?
and i don't think it's <c:url value='/' />, not <c:url value='/table' />

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.