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
Note:- Maintain the order
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.pristine.controller" />
add in jsp
Keep all js and css files in WebContent\resources folder
Second way
Declare in web.xml like below
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>
0 comments: