@ModelAttribute 注解(8)

2020-03-17 18:30:12 浏览数 (1)

@ModelAttribute 注释方法

代码语言:javascript复制
@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","aa@aa.com",12);
            System.out.println("从数据库中获取一个对象:"   user);
            map.put("user",user);
        }
    }
}
代码语言:javascript复制
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(){}
}
代码语言:javascript复制
<!--
    模拟修改操作
    1. 原始数据:1,Tom,aa@aa.com,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="aa@aa.com">
    age:<input type="text" name="age" value="12">
    <input type="submit" name="Submit">
</form>

@ModelAttribute 注释参数

@ModelAttribute("user") User user注释方法参数,参数user的值来源于testModelAttribute03()方法中的model属性。

代码语言:javascript复制
@ModelAttribute("user")
    public User testModelAttribute03(){
        return new User(1,"aa","123","aa@aa.com",12);
    }

    @RequestMapping("/testModelAttribute01")
    public String testModelAttribute04(@ModelAttribute("user") User user){
        user.setUsername("bb");
        return "success";
    }

0 人点赞