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
Firefox-CSS: Border-Radius Issue for Pseudo-Element "Before"
Why Does Display Inline-Block Match Height of Text
Asp.Net MVC Navbar-Brand to Header Text Color
Ie 8. Gradient Background+Image
How to Give Internet Explorer Different CSS Lines
Scrolling Effect: Slow at First Than It Goes Fast
Optimize Font Awesome for Only Used Classes
Pure CSS Triangle with Semi-Transparent Border. Possible
Does The Order of Styles Matter
Bootstrap, Making Responsive Changes to Layout
CSS Not Loading in Firefox, Opera and Ie - Chrome and Safari Works Just Fine
Visited Links Color Not Changing in Ie 8
Center Div in Parent with Only Min-Height, and Child May Without Height & with Relative Position
How to Pass a Parameter to a CSS Class Using Less
Style Different Characters Inside: :After Content