springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Spring Boot Annotations How to Upload a File in Spring Boot JSP How to create a Spring Boot project in VS Code How to create a Spring Boot project in IntelliJ IDEA How to create a Spring Boot project in STS How to Create and Setup Spring Boot Project in Eclipse Spring Boot CRUD Operations with JSP Spring Boot Pagination & Sorting with REST API Spring Boot CRUD with Thymeleaf Sending Mail Using Gmail SMTP Server In Spring Boot
Spring Boot

Spring MVC with Spring Boot Configure

Last Updated: 16-02-2022 Springjava

Spring Framework provides us to develop web applications through the Spring MVC module. Developing web application in Spring MVC is need lots of configurations like

• We will configure a Server
• We will configure the DispatcherServlet  FrontController in the “web.xml” file 
• We will configure the resources in the “dispatcher-servlet.xml” configuration file

In the Spring MVC, we can do configuration with XML-based or Java-based. It will always make a bit of confusion in our minds when developing web applications in the Spring MVC.

These will take lots of time to set up and configure Spring MVC application development. Then web applications in Spring MVC take time to develop.

So, Spring Boot gives a solution to these problems when we use Spring MVC and Spring Boot for developing web applications. Spring Boot provides embedded server support that will save our time on the server configuration for the web application development. If we need the external server configuration for the application it is also supported. There is no confusion about whether we will go Java-based or XML-based configuration or Java annotation with XML config for developing web applications. Because Spring Boot follows a Java-based configuration. DispatcherServlet configuration is the default provided by Spring Boot for web application development. We configure other resources through the “application.properties” file or Java class like view page, CSS, static image, etc.

Spring Boot applications are run through an embedded server or external server. It is based on our requirements.

The Spring Boot web application development is fast as compared to Spring MVC web application development and which configuration is suitable for our application.

In this application, we can configure Spring MVC and Spring Boot App through the “application.properties” file.

Table of content

1. Keep eclipse IDE ready
2. Create a Spring Boot Starter Project(Select Spring Web dependency)
3. Create a view page
4. Create  a Controller class
5. Define View Resolver Path in application.properties
6. Run the Project

1. Keep eclipse IDE ready
2. Create a Spring Boot Starter Project(Select Spring Web dependency)

   spring_mvc_with_spring_boot

Maven Dependency

  <?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>SpringBootWithSpringMVC</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>SpringBootWithSpringMVC</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

3. Create a view page
home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<body>
<b> Hello Spring Boot App</b>
</body>
</html>

→ Create under the “WEB-INF/view”  folder.

4. Create  a Controller class
HomeController.java

package com.example.controller;

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

@Controller
public class HomeController {
	@GetMapping("/")
	public String msg() {
     //return a view page name
    return"home";
	}
}

→ @Controller: Specialization of the "@Component" annotation, useful to configure classes as the controller of the Spring MVC app to process requests given by the clients.

→ @GetMapping: Specialization of @RequestMapping  with getting type request handler and shortcut of @RequestMapping(method=RequestMehod.GET).

Define View Resolver Path in application.properties file
application.properties

# the servlet path
server.servlet.context-path= /demo_mvc
# MVC view prefix
spring.mvc.view.prefix=/WEB-INF/view/
# MVC view suffix
spring.mvc.view.suffix=.jsp
# Embedded Tomcat server port
server.port=8888

6. Run the Project

Right-click on this class i.e., SpringBootWithSpringMvcApplication(automatic generated main class) of this project then click Run as  Java Application(Embedded Server).
Go to a web browser and type this URL “http://localhost:8080/demo_mvc/”

Output

spring_mvc_with_spring_boot

Conclusion

This example is explained:

   • How to configure the Spring MVC in the Spring Boot?

   • How to define the view resolver path and servlet path in the application.properties file?

   • What is the use of @GetMapping?

 

What is Spring Boot How to Create a Spring Boot Project in IDE Spring MVC with Spring Boot Configure

Leave your thought here

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

springjava_com
  • springjavateam@gmail.com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
Subscribe to Blog via Email
Subscribe
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us