1. 程式人生 > 程式設計 >Springmvc獲取前臺請求資料過程解析

Springmvc獲取前臺請求資料過程解析

1)基本資料型別或String,在方法引數中定義引數,引數名與請求傳遞資料名一致即可自動封裝;

// RequestMapping:指定方法對應的請求地址
  //return:頁面地址,表示方法執行完成之後跳轉到對應的頁面(轉發)
  //springmvc:接收請求引數,直接在方法的引數中定義名稱與傳遞引數名一致的形參即可
  //name:會自動接收請求傳遞的name值
  @RequestMapping("/hello")
  public String hello(String name,Integer age){
    System.out.println("name:"+name+",age:"+age);
    return "index.jsp";
  }

啟動tomcat,在瀏覽器位址列輸入

http://localhost:8086/hello?name=tom&age=18

即可檢視結果;

2)物件型別的,在方法引數中定義物件,與物件屬性名一致的資料,會自動封裝進物件;

public class User {

  private Integer id;
private String name;
private Integer age;
private String sex;
private String addr;
}

並對其get和set,再寫一個form表單提交資訊的jsp頁面;

<form action="/saveUser" method="post">

使用者名稱:<input type="text" name="name"/><br/>
年齡:<input type="text" name="age"/><br/>
性別:<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女">女<br/>
地址:<input type="text" name="addr"><br/>
<input type="submit" value="註冊"/>
</form>

再在controller中測試結果;

    @RequestMapping("/saveUser")
    public String saveUser(User user){
       System.out.println("User:"+user);
       return "index.jsp";
    }

3)接收陣列的情況。在方法中定義陣列;

4)物件中的陣列和集合可以接收與集合屬性同名的多個請求資料;

  //通過物件來接收前臺的多個同名數據,兩種方式,一種是陣列,一種是List集合;
  //private String[] hobbies;
   private List<String> hobbies;
  public String[] getHobbies() {
    return hobbies;
  }

  public void setHobbies(String[] hobbies) {
    this.hobbies = hobbies;
  }

  public List<String> getHobbies() {
    return hobbies;
  }

  public void setHobbies(List<String> hobbies) {
    this.hobbies = hobbies;
  }

jsp頁面中新增愛好屬性:

愛好: <input type="checkbox" name="hobbies" value="打籃球"/>打籃球
<input type="checkbox" name="hobbies" value="打遊戲"/>打遊戲
<input type="checkbox" name="hobbies" value="敲程式碼"/>敲程式碼
<input type="checkbox" name="hobbies" value="學習"/>學習<br/>

再在controller中測試結果;

@RequestMapping("/saveUser")
  public String saveAdmin(User user,String[] hobbies){
    System.out.println("user:"+user);
    System.out.println(Arrays.toString(hobbies));
    return "index.jsp";
  }

5)物件中的物件。 前臺指定name 時,屬性.屬性;

要測試物件中的物件,那我們要另外再建立一個物件,然後把這個物件當成屬性新增到User.java中去;

public class Role {
  private String name;
  private Integer id;
  @Override
  public String toString() {
    return "Role{" +
        "name='" + name + '\'' +
        "id='" + id + '\'' +
        '}';
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
}

User.java中:

 //用來接收前臺的資料
  private Role role;
  public Role getRole() {
    return role;
  }

  public void setRole(Role role) {
    this.role = role;
  }

jsp檔案中:

<%-- 將屬性封裝到物件的物件屬性中 屬性名.屬性--%>
角色:<select name="role.name" id="role">
<option value="管理員">管理員</option>
<option value="超級管理員">超級管理員</option>
<option value="測試賬號">測試賬號</option>
</select><br/>
<input type="hidden" name="role.id" value="100"/><br/>

測試:

public String saveUser(User user,String[] hobbies){
    System.out.println("User:"+user);
    System.out.println(Arrays.toString(hobbies));
    System.out.println("role:"+user.getRole()); 
    return "index.jsp"; 
}

6)將資料封裝到map中。在方法中定義HashMap引數,並在前面新增@RequestParam註解;

7)封裝到物件中的map,前臺指定name為 map屬性名[key值];

User.java中:

  //通過map接收前臺的值
  private Map<String,String> conditions;
  public Map<String,String> getConditions() {
    return conditions;
  }
  public void setConditions(Map<String,String> conditions) {
    this.conditions = conditions;
  }

jsp檔案中:

<%--將資料封裝到物件中的map,map屬性名[key]--%>
查詢條件:<input type="text" name="conditions[age]"/><br/>
查詢條件:<input type="text" name="conditions[sex]"/><br/>
<input type="submit" value="註冊"/>

測試:

//map :將所有請求資料封裝到map中
  // @RequestParam("name"):指定向請求中獲取資料 ==>request.getParameter
  //required = false:是否必傳  defaultValue:預設值
  @RequestMapping("/saveUser")
  public String saveUser(User user,String[] hobbies,@RequestParam(name="name",required = false,defaultValue = "username") String username,@RequestParam HashMap map){
    System.out.println("User:"+user);
    System.out.println("role:"+user.getRole());
    System.out.println(Arrays.toString(hobbies));
    System.out.println("map:"+map);
    System.out.println("username:"+username);    System.out.println("conditions:"+user.getConditions());    return "index.jsp"; }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。