This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

Web technology

We know that the common Web technology is also website development, divided into static website, and dynamic website, dynamic website technology has three common, are JSP Java Web, Asp c# web, PHP web but they all correspond to the request, the response the response is the same and the MVC framework that we use to develop dynamic web sites in Java web is called springmvc, Of course, we are using SpringBoot, which is just an integrated framework for the Spirng family bucket. It is not a new framework at all. It is still Spirng + SpringMVC inside

Multiple ways of passing parameters

Traditional parameter passing

We know that the Controller method is going to inject the HttpServletRequest object for me. We can get the parameters directly from request.getParameter(” parameter name “).

@RequestMapping("/test01")
public ModelAndView test01(HttpServletRequest request){
      String username = request.getParameter("username");
      String password = request.getParameter("password");

      System.out.println(username);
      System.out.println(password);
      return null;
}
Copy the code

Simple type parameter mapping

  1. If the request parameter has the same name as the Controller method parameter, it can be accepted directly

  2. If the request parameter has a different name than the Controller method parameter, you can use the @requestParam annotation to precede the parameter and set the corresponding parameter name


Note that this can only be basic data types such as string, int, long, Boolean, etc

@RequestMapping("/test02_1")
public ModelAndView test02_1(String username,String password){
      System.out.println(username);
      System.out.println(password);
      return null;
}
@RequestMapping("/test02_2")
public ModelAndView test02_2(@RequestParam("username") String name,@RequestParam(value = "password",defaultValue = "1234987") String pwd){
      RequestParam (@requestparam) cannot be null
      // Required: indicates whether the value must be passed
      // defaultValue: the defaultValue SpringMVC gives to the request parameter when there is no such parameter
      System.out.println(name);
      System.out.println(pwd);
      return null;
}
Copy the code

Complex object mapping

Of course, in the actual project, we will have many parameters, generally more than two parameters we will be encapsulated into an object, through the object pass parameters, otherwise write one by one will be troublesome, code redundancy, not beautiful, can not be reused

At this point, we can automatically wrap the parameter to the object property of the parameter:

  1. The request parameter must have the same name as the object’s property
  2. In this case, the object is put directly into the request scope, with the type name starting with a lowercase letter
  3. The @ModelAttribute sets the key value that the request parameter is bound to the object and passed to the view page
@RequestMapping("/test03")
public ModelAndView test03(@ModelAttribute("stu") Student student){
    SpringMVC will inject the value of the request parameter into the corresponding attribute of the object based on the same name match. The key value is named @modelAttribute */
      System.out.println(student);
      ModelAndView mv = new ModelAndView();
      mv.setViewName("test2");
      return mv;
}
Copy the code

So if you want a JSON parameter in the body you need to put @requestBody before the parameter and that will do the mapping automatically

 @PostMapping("/register")
    public Result bodyParams(@RequestBody Users users) {
        return ResultResponse.success(users);
    }
Copy the code

Array and collection type parameters

When the parameters passed from the front page are multiple parameters with the same parameter name but different parameter values, they can be directly encapsulated in the method’s array type parameter

For example, parameters passed in during batch deletion

    /* For multiple request parameters with the same parameter name, we can directly use an array as the method parameter or we can use the collection property in the object to receive */
    @DeleteMapping("/del")
    public Result listParams(String[] ids) {
        return ResultResponse.success(ids);
    }
Copy the code

The parameters are passed in Restful style

Restful is a software architecture style, strictly a coding style, that takes advantage of the semantics of the HTTP protocol itself to provide a set of design principles and constraints.

Mainly used for client and server interaction class software, this style of design of software can be more concise, more hierarchical, easier to implement caching mechanisms. In the background, the RequestMapping tag can be passed with {parameter name}, and the @pathvarible annotation should be added before the parameter

@RequestMapping("/delete/{id}")
public ModelAndView test4(@PathVariable("id")Long id){
    System.out.println("delete");
    System.out.println(id);
    return null;
}
Copy the code