一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring MVC常用客戶端參數(shù)接收方式詳解

Spring MVC常用客戶端參數(shù)接收方式詳解

2021-07-17 12:05RainNenya Java教程

這篇文章主要介紹了Spring MVC常用客戶端參數(shù)接收方式詳解,文章主要介紹了幾種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在mvc結(jié)構(gòu)中,控制器組件主要的功能就是接收請求、處理請求、生成響應(yīng),接收客戶端傳來的請求參數(shù)的往往是控制器要做的第一件事。

book實(shí)體類book.java

?
1
2
3
4
5
public class book {
  private integer bookid;
  private string author;
  //生成get、set方法,此處省略
}

一、直接用參數(shù)名匹配請求參數(shù)

客戶端界面(表單):

?
1
2
3
4
5
<form action="/querystring" method="post">
  <input type="text" name="bookid">
  <input type="text" name="author">
  <input type="submit" value="提交">
</form>

controller層:

?
1
2
3
4
5
6
7
8
9
@controller
public class parampassdemo {
  @requestmapping(value="/querystring")
  public string test1(integer bookid, string author) {
    system.out.println("bookid="+bookid+", author="+author);
    //此處返回的地址為(/web-inf/jsp/index.jsp)
    return "index";
  }
}

注意:這里@requestmapping中只有value屬性,value可以省略不寫。

客戶端輸入:123,rose

控制臺輸出:bookid=123, author=rose

二、通過@requestparam注解來指定請求參數(shù)的name

客戶端界面(表單):

?
1
2
3
4
5
<form action="/querystringwithspecname" method="post">
  <input type="text" name="bookid" value="321">
  <input type="text" name="author" value="jack">
  <input type="submit" value="提交">
</form>

如果表單中的字段與方法中的參數(shù)名一致,可以不需要@requestparam,spring會自動處理。
controller層:

?
1
2
3
4
5
6
7
8
@controller
public class parampassdemo {
  @requestmapping("/querystringwithspecname")
  public string test2((value="bookid",required=false) integer id, @requestparam("author") string name) {
    system.out.println("bookid="+id+", author="+name);
    return "index";
  }
}

注意:這里@requestmapping中有兩個屬性,value不能省略。

@requestparam將請求地址中的參數(shù)傳遞給目標(biāo)方法,在處理方法入?yún)⑻幨褂每梢园颜埱髤?shù)傳遞給請求方法。
當(dāng)使用@requestparam注解時,設(shè)置客戶端傳遞的請求參數(shù)name="bookid"和@requestparam的value值value="bookid"相匹配后,參數(shù)名int id可以和請求參數(shù)不匹配。

客戶端輸入:321, jack

控制臺輸出:bookid=321, author=jack

客戶端界面(ajax):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<button onclick="clickme()">點(diǎn)我</button>
<script>
  function clickme() {
      $.ajax({
      type : 'post',
      url : "/querystringwithspecname",
      data : {
        "bookid" : 1,
        "author" : "jack"
      },
    });
  }
</script>

controller層:(不變)

客戶端: data:{"author" : "jack"}

控制臺輸出: bookid=null, author=jack(如果bookid為int類型,控制臺會拋出異常)

客戶端: data:{"bookid" : 1}

控制臺輸出: org.springframework.web.bind.missingservletrequestparameterexception: required string parameter 'author' is not present

通過required設(shè)置可選參數(shù),required為false時表示可以不帶參數(shù),為true時表示必須帶參數(shù)(默認(rèn)值為true)。

當(dāng)可選參數(shù)不存在時,spring默認(rèn)將其賦值為空(null),但由于bookid已定義為基本類型int,所以賦值會失敗。解決方法:采用int包裝類integer。

三、使用領(lǐng)域?qū)ο髞斫邮諈?shù)

客戶端界面(表單):

?
1
2
3
4
5
<form action="/querystringwithdomainobj" method="post">
  <input type="text" name="bookid">
  <input type="text" name="author">
  <input type="submit" value="提交">
</form>

controller層:

?
1
2
3
4
5
6
7
8
@controller
public class parampassdemo {
  @requestmapping("/querystringwithdomainobj")
  public string test3(book book) {
    system.out.println("bookid="+book.getbookid()+", author="+book.getauthor());
    return "index";
  }
 }

客戶端輸入:111, bob

控制臺輸出:bookid=111, author=bob

四、url動態(tài)參數(shù)傳遞(路徑參數(shù))

客戶端界面(超鏈接):

?
1
<a href="/book/1" rel="external nofollow" >testpathvariable</a>

controller層:

?
1
2
3
4
5
6
7
8
9
@controller
public class parampassdemo {
  //@pathvariable可以用來映射url中的占位符到目標(biāo)方法的參數(shù)中
  @requestmapping("/book/{bookid}")
  public string test4(@pathvariable("bookid") integer bookid) {
    system.out.println("bookid:" + bookid);
    return "index";
  }
 }

控制臺輸出:bookid:1

@pathvariable 映射 url 綁定的占位符

通過 @pathvariable 可以將 url 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅校簎rl 中的 {xxx} 占位符可以通過@pathvariable(“xxx“) 綁定到操作方法的入?yún)⒅小?/p>

五、使用httpservletrequest獲取請求參數(shù)

客戶端界面(表單):

?
1
2
3
4
5
<form action="/querybook" method="post">
  <input type="text" name="bookid">
  <input type="text" name="author">
  <input type="submit" value="提交">
</form>

controller層:

?
1
2
3
4
5
6
7
8
9
@controller
public class parampassdemo {
  @requestmapping("/querybook")
  public string test5(httpservletrequest request) {
    system.out.println("bookid:" + request.getparameter("bookid"));
    //此處index.jsp界面在web-inf下
    return "redirect:/index.jsp";
  }
 }

客戶端輸入:123

控制臺輸出:用戶id:123

六、跳轉(zhuǎn)到另一個controller方法

客戶端界面(url地址欄):http://localhost:8080/test6?bookid=321

controller層:

?
1
2
3
4
5
6
7
8
9
10
@controller
public class parampassdemo {
  @requestmapping("/test6")
  public string test6(string bookid){
    system.out.println("bookid="+bookid);
    //使用服務(wù)端跳轉(zhuǎn)的方式轉(zhuǎn)向到另一個controller
    //return "forward:querybook?bookid="+bookid;
    return "redirect:queryuser?bookid="+bookid;
  }
 }

控制臺輸出:bookid=321 bookid:321

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://segmentfault.com/a/1190000018282239

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色里番52kkm全彩 | 亚洲AV 中文字幕 国产 欧美 | 国产成人亚洲精品乱码在线观看 | 我不卡影院手机在线观看 | 国产日本韩国不卡在线视频 | 大乳奶水bbw| 久久久久久88色偷偷 | 国产在线步兵一区二区三区 | 男人的j插入女人的p | narutomanga玖辛奈本子 | 国产精品福利短视在线播放频 | 国产精品久久久久久久久久久威 | 日本在线视频网 | 法国女佣系列在线播放 | 午夜免费啪视频观看视频 | 国产高清好大好夹受不了了 | 白丝捆绑调教 | 好吊日在线| 肉宠文很肉到处做1v1 | 99久久综合九九亚洲 | 香蕉免费看一区二区三区 | 国产精品日韩在线观看 | 午夜精品久久久内射近拍高清 | 国产视频99 | 国产成人久视频免费 | 欧美男人天堂 | 深夜网站在线观看 | 9久热这里只有精品视频在线观看 | 动漫美女人物被黄漫在线看 | 手机国产乱子伦精品视频 | 毛片免费视频观看 | 免费真实播放国产乱子伦 | ai换脸杨颖被啪在线观看 | 日韩精品一区二三区中文 | 亚洲国产区中文在线观看 | 五月天精品视频在线观看 | 亚洲AV无码国产精品午夜久久 | 亚洲精品国产专区91在线 | 四虎影院网站 | 成年人视频在线免费观看 | 天美传媒果冻传媒星空传媒 |