Common annotations and usage in spring MVC

Posted by d1223m on Fri, 18 Feb 2022 00:55:33 +0100

 
 

Common annotations and usage in spring MVC

Introduction to MVC

Introduction to spring MVC

Common annotations in spring MVC

@GetMapping

@RequestParam

@RequestBody

@PathVariable

@RequestHeader

@CookieValue

@ModelAttribute

@SessionAttributes

New annotation

@Difference between RequestMapping and @ GetMapping @PostMapping

If this blog is helpful to you, please remember to leave a message + like + collect.

Introduction to MVC

The full name of MVC is Model View Controller, which is the abbreviation of Model View Controller. It is a mode used to design and create the presentation layer of Web applications.

Model: usually refers to our data model. Generally, it is used to encapsulate data.

View: usually refers to our jsp or html. The function is generally to display data. Typically, views are created from model data.

Controller: the part of an application that handles user interaction. The function is generally to handle the logic of the program.

Introduction to spring MVC

Spring MVC is a request driven lightweight Web framework based on Java to implement MVC design model. It is a follow-up product of Spring FrameWork and has been integrated into Spring Web Flow. The Spring FrameWork provides a way to build Web services

Full function MVC module of application. Using Spring pluggable MVC architecture, you can choose to use Spring's Spring MVC framework or integrate other MVC development frameworks when using Spring for WEB development.

Spring MVC has become one of the most popular MVC frameworks, and with spring 3 0 has become the best MVC framework.

Common annotations in spring MVC

@GetMapping

Function: used to establish the correspondence between the request URL and the request processing method

It can appear on the class and request the first level access directory of the URL

It can appear on the method and request the second level access directory of the URL

value: used to specify the URL of the request. It has the same function as the path attribute

Method: used to specify the method of the request

params: used to specify conditions that restrict request parameters

@Controller
//@RequestMapping("SpringMVC/")
public class HelloController {

    //The request method is get. The request parameter must have username
    @RequestMapping(value = "/hello",method = RequestMethod.GET,params = {"username"})
    //@RequestMapping("/hello")
    public String sayHello(){
        System.out.println("SpringMVC hello~~~");
        return "success";
    }

}

@RequestParam

Function: assign the parameter with the specified name in the request to the formal parameter in the controller

value: the name of the request parameter

required: this parameter must be provided in the request parameter. Default value: true, which means it must be provided. If it is not provided, an error will be reported.

@RequestMapping("/testRequestParam")
    //RequestParam -- rename  
    // Attribute value = alias required = required parameters
    public String testRequestParam(@RequestParam(value = "username") String  name){
        System.out.printf(name);
        System.out.println("testRequestParam Yes~~~");
        return "success";
    }

@RequestBody

Function: used to obtain the content of the request body. Directly use to get key = value & key = vaule Structure data. Get request method is not applicable

required: whether there must be a request body. When the value is true, the get request will report an error. If the value is false, the get request will get null.   

 @RequestMapping("/testRequestBody")
    //RequestBody gets the contents in the request body, such as username = Benshan & password = 989 & money = 200
    public String testRequestBody(@RequestBody String body){
        System.out.println("testRequestBody Yes~~~");
        System.out.println(body);
        return "success";
    }

@PathVariable

Role: used to bind placeholders in URLs. There is / delete / {ID} in the URL, and {ID} is the placeholder.

  @RequestMapping("/testPathVariable/{id}")
    //PathVariable uses Restful style, with clear structure and convenient expansion
    public String testPathVariable(@PathVariable(value = "id") String id){
        System.out.println("testPathVariable~~~");
        System.out.println(id);
        return "success";
    }

@RequestHeader

Function: used to get the request message header

value provides the header name

Required: whether this message header is required

  @RequestMapping("/testRequestHeader")
    //testRequestHeader gets the value of the request header
    public String testRequestHeader(@RequestHeader(value = "Accept") String header){
        System.out.println("testRequestHeader~~~");
        System.out.println(header);
        return "success";
    }

@CookieValue

Function: used to pass the value of the specified cookie name into the controller method parameters

value: Specifies the name of the cookie

Required: is this cookie required

 @RequestMapping("/testCookieValue")
    //testRequestHeader gets the value of the request header
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookie){
        System.out.println("testCookieValue~~~");
        System.out.println(cookie);
        return "success";
    }

@ModelAttribute

Function: it can modify methods and parameters. Appears on the method, indicating that the current method will be executed before the method of the controller is executed. Appear on the parameter, get the specified data and assign a value to the parameter

value is the key used to get data

@RequestMapping("/testModelAttribute")
    public String testModelAttribute(){
        System.out.println("testModelAttribute~~~");
        return "success";
    }
    @ModelAttribute
    //Execute before controller execution
    public void showUser(){
        System.out.println("showUser Yes~~~");
    }

@SessionAttributes

Function: used for parameter sharing between multiple execution controller methods

value is used to specify the name of the attribute stored

Type: used to specify the type of data stored

New annotation

@Difference between RequestMapping and @ GetMapping @PostMapping

@GetMapping is a combined annotation, which is the abbreviation of @ RequestMapping(method = RequestMethod.GET).

@PostMapping is a combined annotation, which is the abbreviation of @ RequestMapping(method = RequestMethod.POST).

If this blog is helpful to you, please remember to leave a message + like + collect.

 

Topics: Java Spring mvc