Spring Boot MVC Creating Controllers and Views

 In Spring Boot MVC, creating controllers and views is streamlined compared to traditional Spring MVC, thanks to embedded configurations and auto-configuration features. Here's a step-by-step guide to creating controllers and views in a Spring Boot MVC application.


1. Setting Up a Spring Boot Project

You can create a Spring Boot project via the Spring Initializr or using your preferred IDE (like IntelliJ IDEA or Eclipse).

Dependencies to Include:

  • Spring Web: For building web applications.
  • Thymeleaf (or JSP): For rendering views (templates).

Sample pom.xml Configuration:

<dependencies> <!-- Spring Boot Starter for Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Thymeleaf for View Rendering --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>

2. Creating a Controller

Create a controller class to handle incoming requests.

Example HomeController

package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/welcome") public String welcome(Model model) { model.addAttribute("message", "Welcome to Spring Boot MVC!"); return "welcome"; // Logical view name (maps to 'welcome.html' in templates folder) } }

Explanation:

  • @Controller: Indicates that this class is a Spring MVC controller.
  • @GetMapping("/welcome"): Maps HTTP GET requests to the /welcome URL.
  • Model: Used to pass data to the view.
  • return "welcome": Refers to a template named welcome.html (Thymeleaf) or welcome.jsp.

3. Creating a View

Create a view template to display the data returned by the controller.

Thymeleaf View (welcome.html)

Place the HTML file in the resources/templates directory.

<!-- src/main/resources/templates/welcome.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Welcome Page</title> </head> <body> <h1 th:text="${message}">Default Welcome Message</h1> </body> </html>

Explanation:

  • th:text="${message}": Displays the value of the message attribute passed from the controller.

4. Running the Application

Main Application Class

Spring Boot automatically configures the embedded Tomcat server. Here's a basic application class:

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

5. Project Structure

Your project structure should look like this:

src/ │-- main/ │ ├── java/ │ │ └── com.example.demo/ │ │ ├── DemoApplication.java │ │ └── controller/ │ │ └── HomeController.java │ └── resources/ │ └── templates/ │ └── welcome.html └── pom.xml

6. Accessing the Controller

Run your application and open your browser to:

http://localhost:8080/welcome

You should see the message: "Welcome to Spring Boot MVC!"


Using JSP Instead of Thymeleaf

If you prefer JSP instead of Thymeleaf:

  1. Add Dependencies in pom.xml:

    <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
  2. Configure application.properties:


    spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp
  3. Create JSP File:

    • Place welcome.jsp in src/main/webapp/WEB-INF/views/.

Summary

  • Controllers handle requests using @GetMapping and other annotations.
  • Views (Thymeleaf or JSP) render the model data.
  • Spring Boot's embedded Tomcat makes deployment seamless.

This setup provides a clear, MVC-structured approach to building web applications in Spring Boot.

Comments