SpringMVC-Common annotations

1, RequestParam annotation

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

Attributes:

          value: the name in the request parameter

          required: Whether this parameter must be provided in the request parameters, the default value is true

@Controller
public class Controller01 {
    @RequestMapping("/hello2")
    public String getRequestParam(@RequestParam(name="username") String name){
        System.out.println("第一个springmvc程序");
        System.out.println(name);
        return "success";
    }
}

2. RequestBody annotation 

Function: used to obtain the content of the request body (note: the get method is not allowed)

Attribute: required: whether there must be a request body, the default value is true 

Code:

<form action="hello" method="post">
        姓名:<input type="text" name="name"/><br>
        年龄:<input type="text" name="age"/><br>
        生日:<input type="text" name="date"><br/>
        <input type="submit" value="提交">
 </form>
@Controller
public class Controller01 {
    @RequestMapping("/hello")
    public String Hello(@RequestBody String body){
        System.out.println("第一个springmvc程序");
        System.out.println(body);
        return "success";
    }

}

Output result:

The first springmvc program
       name=%E5%8F%B6%E6%88%90%E6%89%AC&age=21&date=2021-04-14 

3. ModelAttribute annotation

Application scenarios 

       When the submitted form data is not complete entity data, ensure that the fields not submitted use the original data of the database.

effect 

       Appear on the method : Indicates that the current method will be executed before the controller method is executed .

<form action="modelAttribute" method="post">
   姓名:<input type="text" name="name"/><br>
   年龄:<input type="text" name="age"/><br>
   <input type="submit" value="提交">
</form>

Controller class:

The modified method has a return value

@Controller
public class Controller01 {

    @RequestMapping("/modelAttribute")
    public String modelAttribute(User user){
        System.out.println("第一个springmvc程序");
        System.out.println(user);
        return "success";
    }

    @ModelAttribute
    public User method(String name){
        User u = new User();
        u.setName(name);
        u.setAge(20);
        u.setDate(new Date());
        return u;
    }
}

 The name and age submitted on the previous page have values, but the date is null, so the @ModelAttribute annotation method can attach a value to this date and return the object u to the controller with the attribute value.

Appears on the parameter : Get the specified data to assign the parameter

The decorated method does not return a value

@Controller
public class Controller01 {
    @RequestMapping("/modelAttribute")
    public String modelAttribute(@ModelAttribute("user") User user){
        System.out.println("第一个springmvc程序");
        System.out.println(user);
        return "success";
    }
  
    @ModelAttribute
    public void method(String name, Map<String,User>map){
        User u = new User();
        u.setName(name);
        u.setAge(20);
        u.setDate(new Date());
        map.put("user",u);
    }

}

 On the parameter, get the specified data parameter and assign it to the User user.

 

Guess you like

Origin blog.csdn.net/weixin_43725517/article/details/108983649