3

I am new to Spring MVC and i am having a problem with CSS. When the URL ends with slash CSS does not work.
link goes like this
<link rel="stylesheet" href="themes/style.css">
mvc:resources mapping
<mvc:resources mapping="/themes/**" location="/WEB-INF/themes/"/>
and requestMapping goes like this

@RequestMapping("/login")
public ModelAndView loginPage() {

    ModelAndView model = new ModelAndView("login");

    return model;
}

So the problem is when i enter a URL like ../login the css loads normally, but when i enter ../login/ with ending slash, then the css does not load. Well there are many similar questions in here, but none of them is for Spring MVC.

2 Answers 2

6

Instead of

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

try this:

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

When you use href="themes/style.css" then for url like: .../login/ the request url for css file looks like:

.../login/themes/style.css

which is incorrect. When you use href="/themes/style.css" then it always should result with:

.../themes/style.css

Update:

If it is jsp page, then add <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> on top of the page and change:

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

into

<link rel="stylesheet" href="<c:url value="/themes/style.css" />">
Sign up to request clarification or add additional context in comments.

1 Comment

in this case CSS stops loading for ../login too.
1

it will work 100%

step 1

<mvc:resources location="/WEB-INF/assets/" mapping="/resources/**"></mvc:resources>

setp 2

<spring:url var="css" value="/resources/css/"/>
<spring:url var="js" value="/resources/js/"/>

<spring:url var="image" value="/resources/image/"/> 

add a slash after value

step 3

add your style sheet like this

 <link rel="stylesheet" 
    href="${css}/common/bootstrap.min.css">

Comments

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.