@ModelAttribute 註解(8)

  • 2020 年 3 月 17 日
  • 筆記

@ModelAttribute 注釋方法

@Controller  @RequestMapping("/springmvc")  public class TestController {      /**       * 運行流程:@ModelAttribute注釋的方法會在此controller每個方法執行前被執行       * 1. 執行 @ModelAttribute 修飾的方法:模擬資料庫取出行為,將user對象存儲到map中,鍵為 user       * 2. 然後在渲染頁面時,模擬回顯數據,將model中的數據取出並顯示在頁面,並把表單的請求參數賦給User對象的對應屬性       * 3. sprinngMVC 最後把上述對象傳入目標方法的參數       * 要注意的是:在存入map時,map的鍵值要和被@RequestMapping("/testModelAttribute")修飾的目標方法入參類型的第一個字元串的字元串一致       * @param user       * @return       */      @RequestMapping("/testModelAttribute")      public String testModelAttribute01(User user){          System.out.println("修改: " + user);          return "success";      }        /**       * @ModelAttribute 標記的方法,會在每個目標方法執行之前被SpringMVC調用       * @param id       * @param map       */      @ModelAttribute      public void testModelAttribute(@RequestParam(value = "id",required = false)                                                   Integer id,Map<String,Object> map){            if(id != null){              // 模擬從資料庫中獲取對象              User user = new User(1,"Tom","123456","[email protected]",12);              System.out.println("從資料庫中獲取一個對象:" + user);              map.put("user",user);          }      }  }
public class User {        private Integer id;      private String username;      private String password;        private String email;      private int age;      private Address address;        public void setUsername(String username) {          this.username = username;      }        public String getUsername() {          return username;      }        public void setPassword(String password) {          this.password = password;      }        public String getPassword() {          return password;      }        public void setEmail(String email) {          this.email = email;      }        public String getEmail() {          return email;      }        public void setAge(int age) {          this.age = age;      }        public int getAge() {          return age;      }        public void setAddress(Address address) {          this.address = address;      }        public Address getAddress() {          return address;      }        public void setId(Integer id) {          this.id = id;      }        public Integer getId() {          return id;      }        @Override      public String toString() {          return "User{" +                  "id=" + id +                  ", username='" + username + ''' +                  ", password='" + password + ''' +                  ", email='" + email + ''' +                  ", age=" + age +                  '}';      }        public User(Integer id,String username, String password, String email, int age){          super();          this.username = username;          this.password = password;          this.email = email;          this.age = age;          this.id = id;      }        public User(){}  }
<!--      模擬修改操作      1. 原始數據:1,Tom,[email protected],12      2. 密碼不能被修改      3. 表單回顯,模擬操作直接填寫對應的屬性值    -->  <form action="/springmvc/testModelAttribute" method="post">      <input type="hidden" name="id" value="1">      username:<input type="text" name="username" value="Tom">      email:<input type="text" name="email" value="[email protected]">      age:<input type="text" name="age" value="12">      <input type="submit" name="Submit">  </form>

@ModelAttribute 注釋參數

@ModelAttribute("user") User user注釋方法參數,參數user的值來源於testModelAttribute03()方法中的model屬性。

@ModelAttribute("user")      public User testModelAttribute03(){          return new User(1,"aa","123","[email protected]",12);      }        @RequestMapping("/testModelAttribute01")      public String testModelAttribute04(@ModelAttribute("user") User user){          user.setUsername("bb");          return "success";      }