欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springmvc獲取前臺(tái)請(qǐng)求數(shù)據(jù)過(guò)程解析

 更新時(shí)間:2020年07月01日 15:49:48   作者:等你的夏天  
這篇文章主要介紹了Springmvc獲取前臺(tái)請(qǐng)求數(shù)據(jù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1)基本數(shù)據(jù)類型或String,在方法參數(shù)中定義參數(shù),參數(shù)名與請(qǐng)求傳遞數(shù)據(jù)名一致即可自動(dòng)封裝;

// RequestMapping:指定方法對(duì)應(yīng)的請(qǐng)求地址
  //return:頁(yè)面地址,表示方法執(zhí)行完成之后跳轉(zhuǎn)到對(duì)應(yīng)的頁(yè)面(轉(zhuǎn)發(fā))
  //springmvc:接收請(qǐng)求參數(shù),直接在方法的參數(shù)中定義名稱與傳遞參數(shù)名一致的形參即可
  //name:會(huì)自動(dòng)接收請(qǐng)求傳遞的name值
  @RequestMapping("/hello")
  public String hello(String name,Integer age){
    System.out.println("name:"+name+",age:"+age);
    return "index.jsp";
  }

啟動(dòng)tomcat,在瀏覽器地址欄輸入

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

即可查看結(jié)果;

2)對(duì)象類型的,在方法參數(shù)中定義對(duì)象,與對(duì)象屬性名一致的數(shù)據(jù),會(huì)自動(dòng)封裝進(jìn)對(duì)象;

public class User {

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

并對(duì)其get和set,再寫一個(gè)form表單提交信息的jsp頁(yè)面;

<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="注冊(cè)"/>
</form>

再在controller中測(cè)試結(jié)果;

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

3)接收數(shù)組的情況。在方法中定義數(shù)組;

4)對(duì)象中的數(shù)組和集合可以接收與集合屬性同名的多個(gè)請(qǐng)求數(shù)據(jù);

  //通過(guò)對(duì)象來(lái)接收前臺(tái)的多個(gè)同名數(shù)據(jù),兩種方式,一種是數(shù)組,一種是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頁(yè)面中添加愛好屬性:

愛好: <input type="checkbox" name="hobbies" value="打籃球"/>打籃球
<input type="checkbox" name="hobbies" value="打游戲"/>打游戲
<input type="checkbox" name="hobbies" value="敲代碼"/>敲代碼
<input type="checkbox" name="hobbies" value="學(xué)習(xí)"/>學(xué)習(xí)<br/>

再在controller中測(cè)試結(jié)果;

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

5)對(duì)象中的對(duì)象。 前臺(tái)指定name 時(shí),屬性.屬性;

要測(cè)試對(duì)象中的對(duì)象,那我們要另外再建立一個(gè)對(duì)象,然后把這個(gè)對(duì)象當(dāng)成屬性添加到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中:

 //用來(lái)接收前臺(tái)的數(shù)據(jù)
  private Role role;
  public Role getRole() {
    return role;
  }

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

jsp文件中:

<%-- 將屬性封裝到對(duì)象的對(duì)象屬性中 屬性名.屬性--%>
角色:<select name="role.name" id="role">
<option value="管理員">管理員</option>
<option value="超級(jí)管理員">超級(jí)管理員</option>
<option value="測(cè)試賬號(hào)">測(cè)試賬號(hào)</option>
</select><br/>
<input type="hidden" name="role.id" value="100"/><br/>

測(cè)試:

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)將數(shù)據(jù)封裝到map中。在方法中定義HashMap參數(shù),并在前面添加@RequestParam注解;

7)封裝到對(duì)象中的map,前臺(tái)指定name為 map屬性名[key值];

User.java中:

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

jsp文件中:

<%--將數(shù)據(jù)封裝到對(duì)象中的map,map屬性名[key]--%>
查詢條件:<input type="text" name="conditions[age]"/><br/>
查詢條件:<input type="text" name="conditions[sex]"/><br/>
<input type="submit" value="注冊(cè)"/>

測(cè)試:

//map :將所有請(qǐng)求數(shù)據(jù)封裝到map中
  // @RequestParam("name"):指定向請(qǐng)求中獲取數(shù)據(jù) ==>request.getParameter
  //required = false:是否必傳  defaultValue:默認(rèn)值
  @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"; }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot自動(dòng)加載配置的原理解析

    Springboot自動(dòng)加載配置的原理解析

    Springboot遵循“約定優(yōu)于配置”的原則,使用注解對(duì)一些常規(guī)的配置項(xiàng)做默認(rèn)配置,減少或不使用xml配置,讓你的項(xiàng)目快速運(yùn)行起來(lái),這篇文章主要給大家介紹了關(guān)于Springboot自動(dòng)加載配置原理的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 一文詳解Spring?security框架的使用

    一文詳解Spring?security框架的使用

    Spring?Security是一個(gè)基于Spring框架的安全認(rèn)證和授權(quán)框架,它提供了一套全面的安全解決方案,可以在Web應(yīng)用、移動(dòng)應(yīng)用和Web服務(wù)等不同場(chǎng)景下使用。本文就來(lái)詳細(xì)聊聊它的使用吧
    2023-03-03
  • 詳解JNI到底是什么

    詳解JNI到底是什么

    JNI是Java Native Interface的縮寫,通過(guò)使用 Java本地接口書寫程序,可以確保代碼在不同的平臺(tái)上方便移植。從Java1.1開始,JNI標(biāo)準(zhǔn)成為java平臺(tái)的一部分,它允許Java代碼和其他語(yǔ)言寫的代碼進(jìn)行交互
    2021-06-06
  • JAVA實(shí)現(xiàn)往字符串中某位置加入一個(gè)字符串

    JAVA實(shí)現(xiàn)往字符串中某位置加入一個(gè)字符串

    這篇文章主要介紹了JAVA實(shí)現(xiàn)往字符串中某位置加入一個(gè)字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • MyBatis-plus中的模糊查詢解讀

    MyBatis-plus中的模糊查詢解讀

    這篇文章主要介紹了MyBatis-plus中的模糊查詢解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • java基于servlet使用組件smartUpload實(shí)現(xiàn)文件上傳

    java基于servlet使用組件smartUpload實(shí)現(xiàn)文件上傳

    這篇文章主要介紹了java基于servlet使用組件smartUpload實(shí)現(xiàn)文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • java中線程安全的list詳細(xì)特性和用法

    java中線程安全的list詳細(xì)特性和用法

    這篇文章主要給大家介紹了關(guān)于java中線程安全的list詳細(xì)特性和用法的相關(guān)資料,Java中有多種線程安全的List,其中比較常用的有Vector、Collections.synchronizedList()和CopyOnWriteArrayList三種方式,需要的朋友可以參考下
    2024-03-03
  • SpringMVC接收前臺(tái)傳遞過(guò)來(lái)的值的實(shí)例

    SpringMVC接收前臺(tái)傳遞過(guò)來(lái)的值的實(shí)例

    下面小編就為大家分享一篇SpringMVC接收前臺(tái)傳遞過(guò)來(lái)的值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • 建議你使用LocalDateTime而不是Date哦

    建議你使用LocalDateTime而不是Date哦

    這篇文章主要介紹了建議你使用LocalDateTime而不是Date,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • jmeter正則表達(dá)式實(shí)例詳解

    jmeter正則表達(dá)式實(shí)例詳解

    正則表達(dá)式就是記錄文本規(guī)則的代碼。學(xué)習(xí)正則表達(dá)式最好就是從實(shí)例下手。下面我們通過(guò)實(shí)例代碼給大家介紹jmeter正則表達(dá)式的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-12-12

最新評(píng)論