Handling static content in Spring MVC

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.


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

Now link the static content to ur application as follows.


<%@ 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" />

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


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

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.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s