@Controller Annotation in Spring Boot: Principles, Usage and Examples

@Controller Annotation in Spring Boot: Principles, Usage and Examples

foreword

Spring Boot is a framework for rapid development of Spring applications, which provides many useful functions and features. Among them, the @Controller annotation is a commonly used annotation, which can mark a Java class as a controller in Spring MVC. This article will introduce the principle, usage and examples of the @Controller annotation in Spring Boot.

insert image description here

The principle of @Controller annotation

In Spring MVC, the @Controller annotation is used to identify a Java class as a controller. Controllers are responsible for receiving requests, processing them, and returning responses. Specifically, classes annotated with @Controller will be automatically scanned by Spring MVC and registered as a controller.

The @Controller annotation is similar to the @Component annotation, which marks a class as a Spring component. However, the @Controller annotation also has the following features:

  • Automatically map URLs. The @Controller annotation can automatically map requests to controller methods.
  • View resolution is supported. The @Controller annotation can resolve the return value of the method into a view and render it to the client.

Usage of @Controller annotation

The steps to use the @Controller annotation are as follows:

1. Create a controller class

First, you need to create a Java class and annotate it with the @Controller annotation.

@Controller
public class HelloController {
    
    

    @GetMapping("/hello")
    public String hello() {
    
    
        return "hello";
    }

}

2. Write the controller method

In the controller class, you need to write the controller method. Controller methods process requests and return responses. Controller methods can use annotations provided by Spring MVC to specify the requested URL, request method, and other parameters.

@GetMapping("/hello")
public String hello() {
    
    
    return "hello";
}

In the above code, the @GetMapping annotation is used to specify the requested URL, ie /hello. The return value of the method is "hello", indicating that the view named "hello" is to be rendered.

3. Configure the view resolver

In Spring MVC, the return value of a controller method can be a view name or a ModelAndView object. In order to resolve view names to actual views, a view resolver needs to be configured.

@Bean
public ViewResolver viewResolver() {
    
    
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

In the above code, an InternalResourceViewResolver object is created, and the prefix is ​​set to /WEB-INF/views/, and the suffix is ​​set to .jsp. This way, when the controller method returns "hello", Spring MVC will resolve to /WEB-INF/views/hello.jsp.

4. Start the application

Finally, you need to start the application and visit http://localhost:8080/hello to see the content of the "hello" view.

@SpringBootApplication
public class MyApp {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(MyApp.class, args);
    }

}

Example of @Controller annotation

Below is a sample project annotated with @Controller.

1. Create a project

Create a new Spring Boot project using Spring Initializr and add the following dependencies:

  • Spring Web
  • Thymeleaf

2. Create the controller class

Create a GreetingController class and annotate this class with the @Controller annotation.

@Controller
public class GreetingController {
    
    

    @GetMapping("/greeting")
    public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
    
    
        model.addAttribute("name", name);
        return "greeting";
    }

}

In the above code, the @GetMapping annotation is used to specify the requested URL, ie /greeting. In the parameters of the method, the @RequestParam annotation is used to specify the request parameters. If no name parameter is passed in the request, the default value "World" is used. The return value of the method is "greeting", indicating that the view named "greeting" is to be rendered.

3. Create a view template

In the src/main/resources/templates directory, create a Thymeleaf template file called greeting.html.

<!DOCTYPE html>
<html>
<head>
    <title>Greeting</title>
</head>
<body>
    <h1>Hello, [[${name}]]!</h1>
</body>
</html>

In the above code, [[${name}]] represents a Thymeleaf expression used to obtain the value of the name parameter passed in the controller method.

4. Start the application

Finally, start the application and visit http://localhost:8080/greeting?name=Jack, you can see the content of "Hello, Jack!".

@SpringBootApplication
public class MyApp {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(MyApp.class, args);
    }

}

Summarize

The @Contoller annotation is one of the commonly used annotations in Spring Boot, which can mark a Java class as a controller in Spring MVC. Controllers are responsible for receiving requests, processing them, and returning responses. This article introduces the principle, usage and examples of the @Controller annotation, hoping to help readers better understand and use this annotation.

Guess you like

Origin blog.csdn.net/JasonXu94/article/details/131455382