How to Map CSS and Js File from Jsp File in Spring Mvc

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.

Spring MVC import css and js in jsp page

I had faced similar issue, I tried to implement the spring part from the below page and it worked,

http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/

The mvc:resources mapping is the key concept.

Regards

How to use css in the jsp in Spring MVC project?

this css resource issue

please try below

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

and you can see this resource if be load by bowaser.

How to import css/js in spring MVC application jsp page

Put the CSS, JS, image files, etc., into a directory (which may contain subdirectories) in your web site, and than map the requests to that directory:

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

In this example the files are in the public-resources directory, and you refer to them using /resources in the link in your JSP.

For more information see Spring docs

How to include external JS file in JSP file in Spring WebMVC framework?

You might want to add ResourceHandler to resolve your static resources like js/css directory

public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}

This method apply for java configuration class extends WebMvcConfigurerAdapter

The xml version should look like this

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

This will resolve any .js file under /webapp/js/ directory, using something like below in .jsp file

<script src="js/custom.js"></script>

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