使用@ModelAttribute來獲取請求中的數據


@ModelAttribute注解用於將方法的參數或者方法的返回值綁定到指定的模型屬性上,並返回給web視圖。

  在實際工作中,有些時候我們在修改數據的時候可能只需要修改其中幾個字段,而不是全部的屬性字段都獲取,那么當提交屬性的時候,從form表單中獲取的數據就有可能只包含了部分屬性,此時再向數據庫更新的時候,肯定會丟失屬性,因為對象的封裝是springmvc自動幫我們new的,所以此時需要先將從數據庫獲取的對象保存下來,當提交的時候不是new新的對象,而是在原來的對象上進行屬性覆蓋,此時就需要使用@ModelAttribute注解。

定義User bean對象:

package com.test.bean;

public class User {
    private Integer id;
    private String name;
    private String password;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

定義控制器UserController:

package com.test.controller;

import com.test.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    Object o1 = null;
    Object o2 = null;
    Object o3 = null;

    @RequestMapping("update")
    public String update(@ModelAttribute("user") User user,Model model){
        System.out.println(user);
        o2 = model;
        //可以看到所有的model都是同一個對象
        System.out.println(o1==o2);
        //可以看到存儲的user對象也是同一個
        System.out.println(user == o3);
        return "output";
    }

    @ModelAttribute
    public void MyModelAttribute(Model model){
        o1 = model;
        User user = new User();
        user.setId(1);
        user.setName("user01");
        user.setAge(12);
        user.setPassword("123");
        model.addAttribute("user",user);
        System.out.println("modelAttribute:"+user);
        o3 = user;
    }
}

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: root
  Date: 2020/3/11
  Time: 13:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="update" method="post">
    <input type="hidden" value="1" name="id">
    姓名:user01<br>
    密碼:<input type="text" name="password"><br>
    年齡:<input type="text" name="age"><br>
    <input type="submit" value="提交">
  </form>
  </body>
</html>

總結:

1、方法的參數使用參數的類型首字母小寫,或者使用@ModelAttribute("")的值

2、先看之前是否在model中設置過該屬性值,如果設置過就直接獲取

3、看@SessionAttributes注解標注類中的方法是否給session中賦值,如果有的話,也是直接獲取,沒有報異常


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM