Hey guys in this post, we will discuss spring @Controller
annotation with example.
Table of Contents
Overview
@Controller
annotation acts as a controller in the MVC design pattern. It indicates that the particular class serves the role of controller.
It contains methods that are annotated with @RequestMapping
annotation with URL mappings that are triggered for web requests.
It works with view technology, so the method returns ModelAndView
.
@Controller
public class HomeController {
@RequestMapping("/name")
public ModelAndView displayName () {
ModelAndView mav = new ModelAndView("hello-world");
//add data to model and view
return mav;
}
}
Watch the video
Example on @Controller
The best way to understand @Controller
is by looking at the example
Create spring boot project
There are many different ways to create a spring boot application, you can follow the below articles to create one –
>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA
Add maven dependencies
Open pom.xml
and add the following dependencies –
<?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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.bushansirgur</groupId>
<artifactId>controllerannotation</artifactId>
<version>v1</version>
<name>controllerannotation</name>
<description>Spring boot controller annotation demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
spring-boot-starter-web
dependency for building web applications using Spring MVC.
It uses the tomcat as the default embedded container. spring-boot-devtools
dependency for automatic reloads or live reload of applications. The spring-boot-starter-thymeleaf
is a starter for building Spring MVC applications with Thymeleaf.
Create a controller
Create HomeController.java
inside the in.bushansirgur.springboot.controller
package and add the following content
package in.bushansirgur.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping("/name")
public ModelAndView displayName () {
ModelAndView mav = new ModelAndView("hello-world");
mav.addObject("name", "Bushan");
return mav;
}
}
We have annotated the class with @Controller
annotation, which indicates that the class serves the role of controller. The method returns the ModelAndView
, which contains the view name and data that should display in the view template.
Create a view
Create hello-world.html
inside the templates
folder of src/main/resources
and add the following content
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p th:text="'My name is '+${name}"></p>
</body>
</html>
Inside the <p>
element we are accessing the data using ${}
that we have added to the ModelAndView
.
Run the app
Run the application using the below maven command –
mvn spring-boot:run
Open the browser and navigate to the URL http://localhost:8080/name