Handling static content in Spring MVC

Supun Dharmarathne
technodyne
Published in
2 min readOct 8, 2012

Since Spring MVC is built in framework, there is an specific way to deal with it in some scenarios. Adding the static content like css,js, images is little bit tricky with spring mvc. Before adding these resources, there should be an specific folder structure to it. It should be as follows.

src/
springmvc/
java/
HomeController.java
WebContent/
resources/
img/
image.jpg
WEB-INF/
jsp/
index.jsp
web.xml
mvc-dispatcher-servlet.xml

Or as follows.

Everything you need to be static should be kept inside the webapp/resources folder. Now add the configuration to the mvc-dispatcher servlet.xml. Finally it should be like this.

[sourcecode language=”xml”]

<?xml version=”1.0" encoding=”UTF-8"?>
<beans xmlns=”http://www.springframework.org/schema/beans"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc=”http://www.springframework.org/schema/mvc"
xmlns:context=”http://www.springframework.org/schema/context"
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package=”com.supun.springwater.controller” />
<mvc:resources mapping=”/resources/**” location=”/resources/” />

<bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter” />
<bean class=”org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”></bean>
<bean id=”viewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”viewClass”
value=”org.springframework.web.servlet.view.JstlView” />
<property name=”prefix”>
<value>/WEB-INF/pages/</value>
</property>
<property name=”suffix”>
<value>.jsp</value>
</property>
</bean>

</beans>

[/sourcecode]

Now link the static content to ur application as follows.

[sourcecode language=”html”]

<%@ taglib uri=”http://java.sun.com/jsp/jstl/core" prefix=”c” %>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859–1">
<link type=”image/x-icon” href=”./images/favicon.ico” rel=”shortcut icon” lang=”en”/>
<title>Home</title>
<link href=”<c:url value=”/resources/css/view.css” />” rel=”stylesheet” type=”text/css” />
<link href=”<c:url value=”/resources/css/gh-buttons.css” />” rel=”stylesheet” type=”text/css” />

[/sourcecode]

Now add the dependencies for JSTL in your pom.xml as follows.

[sourcecode language=”xml”]

<! — standard.jar →
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<! — JSTL →
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>

[/sourcecode]

Now clean the project and reimport to eclipse using following commands.

mvn eclipse:clean & mvn eclipse:eclipse -Dwtpversion=2.0 . Then run the application in your application server.

--

--