So the question is, how to include external CSS file or Bootstrap CSS file properly into JSP??
It's easy as adding few lines in your SpringMVC 3 XML file! Before that, let's see what the issue is.
When you keep your CSS/ images/ JS files inside your web folder and try to load it in JSP, it goes to Spring's Dispatcher Servlet. There is no mapping for those files and Spring won't load them.
Add the following line to the dispatcher-servlet.xml and put all the static resources like CSS, images & JavaScript files inside a folder inside your web folder as mentioned below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<mvc:annotation-driven/> | |
<mvc:resources mapping="/resources/**" location="resources/"/> |
And the JSP file needs to access the external CSS file as follows. In this example, I've used Bootstrap, but same applicable for your own CSS as well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> | |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> | |
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | |
<html> | |
<head> | |
<title>Spring 3 MVC with External CSS</title> | |
<link href="<c:url value="/resources/css/bootstrap.css" />" rel="stylesheet" type="text/css" /> | |
</head> | |
<body> | |
... | |
</body> | |
</html> |
Now a final tip before finishing- did you notice the first line of the spring config file? Make sure you have the line <mvc:annotation-driven/> in place when you are using annotation based controller definition in your source. Try without that line and you'll have a frustrating situation where the resources will work fine and the JSPs won't be loading properly!
The reason is this. When you add the
No comments:
Post a Comment