Spring MVC Serve HTML JSP Page

Spring MVC Serve HTML JSP Page

In this demo you will see how to return either static HTML or JSP page from Spring MVC application. Please follow steps below:

Summary: To return either html or jsp page from spring mvc don’t include InternalResourceViewResolver bean to return particular extension with page as shown in example below where it says: include suffix with all the resources when we return page from spring controller:

<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
</bean>

Note: As you will see in below example we have removed InternalResourceViewResolver bean form dispatcher-servlet.xml file and adding page with extension from spring mvc controller.

Steps:

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

Spring MVC Serve HTML JSP Page

  • dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
	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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.javahonk.controller" />	

</beans>
  • SpringMVCController:
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 "/static/index.html";
		//return "index.jsp";

	}
}
  • index.html:
<!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>
</head>
<body>
	<h2>Spring MVC Serve HTML Successful</h2>
</body>
</html>
  • Web page output:

Spring MVC Serve HTML JSP Page

  • To return JSP page from controller change controller code as below:
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 "/static/index.html";
		return "index.jsp";

	}
}
  • Web page output:

Spring MVC Serve HTML JSP Page

For more information please read Spring MVC tutorial here.

download Download Project:  SpringMVCHTMLReturn

Leave a Reply

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