0

I am new to Spring mvc. I want to add css and js files in jsp. It's a spring mvc framework. This is my spring-dispatcher-servlet.xml code

<mvc:annotation-driven />
    <mvc:resources location="/resources/**" mapping="/css/**"/>

SpringDemo is my project name folder. I keep my file in SpringDemo/WebContent/resources/css and in the same location i keep my js file also SpringDemo/WebContent/resources/js

Below code is jsp file called RegistrationForm.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

    <link href="WebContent/resources/css/style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="/SpringDemo/js/jquery.min.js"></script>
Roman C
  • 49,761
  • 33
  • 66
  • 176

2 Answers2

1

Well it looks like your resource mapping is not correct, your css and js files are under "/resources" folder right? then the mapping should be

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

The inclusion of the elements in the view have been answered multiple times in stackoverflow itself. You can refer unable to use resources like css/js in spring mvc or How to include js and CSS in JSP with spring MVC

Community
  • 1
  • 1
0

In case you are still looking:

Resource Mapping:

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

Adding JS/CSS in jsp pages:

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/main.css"/>">
<script src="<c:url value="/resources/js/app.min.js"/>"></script>

Note: All of the JS/CSS files must be under src\main\webapp\resources directory.

And if you have added Security, then you need to add this:

<http pattern="/resources/**" security="none"/>
Sanjay Rawat
  • 2,304
  • 15
  • 30