Blog Detail

  • Home
  • Enable MVC in Spring Boot

Enable MVC in Spring Boot

In this post I have posted the steps for to enable the Spring MVC flow in Spring Boot Application. In normal spring application if we want to add the MVC, we should prepare the xml for define the dispatcher servlet its hard to and need to spend some time on it. but in spring boot make it every configuration as easy. because its doing autoconfiguration based on dependency that we are added in pom file. so here no need to add dispatcher servlet.

In your spring boot application if you want to enable the MVC you just follow below steps. that’s really helps you to enable MVC in your spring boot application quickly.

pom.xml

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
           <groupId>org.apache.tomcat.embed</groupId>
	   <artifactId>tomcat-embed-jasper</artifactId>
	</dependency>

application.properties

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

Java Controller

package com.samplecoder.emo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ApplicationController {

	@GetMapping("/index")
	public String index() {
		return "index";
	}
	
}

index.jsp

This index.jsp file should be presented inside the src/main/webapp/WEB-INF/view folder

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello!. This is my spring boot MVC application</h1>

</body>
</html>

After done this changes in your code, you just run the application and check the result here! http://localhost:8080/index

1 Comment

Write a comment