Accessing CSS from Spring Mvc

How to add CSS and JS to Spring mvc project

Move the resource folder under webapp folder. And with this line you can connect your css file.

<link rel="stylesheet" href="/css/styles.css"> 

Similarly write code to connect script like

<script type="text/javascript" src="/js/script.js"></script>

How to include js and CSS in JSP with spring MVC

First you need to declare your resources in dispatcher-servlet file like this :

<mvc:resources mapping="/resources/**" location="/resources/folder/" />

Any request with url mapping /resources/** will directly look for /resources/folder/.

Now in jsp file you need to include your css file like this :

<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">

Similarly you can include js files.

Hope this solves your problem.

cannot load css and js files when using spring mvc

Try any of the below :

With JSTL tag c:url

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

With spring:url

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<spring:url value="/design/css/reset.css" var="reset" />

without any tags

<link href="${pageContext.request.contextPath}/design/css/reset.css" rel="stylesheet"


Related Topics



Leave a reply



Submit