Powered by Blogger.

Tuesday, 16 December 2014

Adding js and css in Spring MVC

By Shambhu Kumar  |  04:01 No comments

For adding js and css in Spring MVC app There are 2 ways.

First Way
we need to declare resources tag in dispatcher-servlet.xml (i.e spring bean configuration file).

Spring configuration file entry



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

Note:- Maintain the order
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <context:component-scan base-package="com.pristine.controller" />

add in jsp


<script type="text/javascript"  
       src="${pageContext.request.contextPath}/resources/js/angular.min.js"></script>
 
Keep all js and css files in WebContent\resources folder

Second way
Declare in web.xml like below


<servlet-mapping>
       <servlet-name>default</servlet-name>
       <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates
TOP