Enable MVC in Spring Boot

In this post I have posted the steps to enable Spring MVC in Spring Boot Application. In normal spring application, if we want to enable the MVC flow, we should prepare the xml to define the dispatcher servlet. But creating the config xml and define the dispatcher servlet is hard, and need to spend more time on it. But in spring boot you can do it easily. Because its doing autoconfiguration based on dependency, that we have injected on pom file. So here no need to add or define the dispatcher servlet configuration.

MVC (Model – View – Controller)

MVC is short term of Model, View and Controller. It is an architecture. It is the best architecture to develop the web based application.

Model: Model mean data. It represents the Java POJO object or JSON data that you have been displaying in the View

View: View is representing the HTML or JSP file. It is a combination of HTML, CSS and Java Script.

Controller: Controller is a back-end java code, it has multiple endpoint, it will be consumes the client request and provide the response in HTML or JSON.

Enable MVC in Spring Boot

How to enable MVC?

In your spring boot application if you want to enable the MVC you just follow below steps. That’s really helpful 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

One Comment on “Enable MVC in Spring Boot”

Comments are closed.