SpringBoot MVC with Example:

In this article let us see an example of MVC (Model View Controller) with SpringBoot.

This is an simple example of SpringBoot MVC, without involving database. We are going to use @RestController with mapping and deploy the application.

Why SpringBoot MVC and Why not Spring MVC?
The main advantage of using SpringBoot for MVC over Spring MVC is,
Spring Boot does this with Zero XML configuration in your project.  We will not be needing deployment descriptors or any other configuration files.

Let us see an example,

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>SpringBootMVC</groupId>
	<artifactId>SpringBootMVC</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>SpringBootMVC</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- Tomcat for JSP rendering -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>

		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

SpringController.java

package com.javainfinite.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringController {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String welcome() {
		return "home";
	}
}

application.properties

spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**

App.java

package com.javainfinite.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello World!
 *
 */
@SpringBootApplication
@ComponentScan("com.javainfinite")
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

home.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>Home Page</title>
</head>
<body>Welcome to SpringBoot MVC
</body>
</html>

Output:

 

Download code here

 

By Sri

One thought on “SpringBoot MVC with Example”

Leave a Reply

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