Spring MVC static file include

Spring MVC static file include

In this demo you will see how to include static folder path and add its file dynamically in Spring MVC application. For this demo we will create sample jsp page and add static css file in it. This application can also use as RESTFul service:

  • Create maven project name: SpringMVCHTMLReturn
  • Final project structure:

Spring MVC static file include

  • web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>		
		<url-pattern>/</url-pattern>		
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>	

</web-app>
  • dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<context:component-scan base-package="com.javahonk.controller" />
	<mvc:resources mapping="/static/**" location="/static/" />
    <mvc:annotation-driven/>
    
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value></value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>	

</beans>
  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC Serve HTML</title>
<link rel="stylesheet" href="../SpringMVCHTMLReturn/static/style.css" />
</head>
<body>
	<h2>Spring MVC Serve JSP: ${message}</h2>
</body>
</html>
  •  SpringMVCController.java:
package com.javahonk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SpringMVCController {
	@RequestMapping(value="/helloWorld.web",method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {

		model.addAttribute("message", "Successful");
		return "index";

	}
	
	@RequestMapping(value="/test")
	public String test(ModelMap model) {

		model.addAttribute("message", "Successful");
		return "index";

	}
}
  • Web page output:

Spring MVC static file include

Spring MVC static file include

Spring MVC static file include

For more information on URL mapping please read this tutorial 

download Download Project:  SpringMVCHTMLReturn

 

 

2 thoughts on “Spring MVC static file include”

Leave a Reply

Your email address will not be published. Required fields are marked *