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>
1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried those. but it doesn't work. if you know please let me know. thank you
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"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.