Spring3 MVC請求參數(shù)獲取的幾種方法
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、秀英網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、秀英網(wǎng)絡(luò)營銷、秀英企業(yè)策劃、秀英品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供秀英建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.chinadenli.net
一、通過@PathVariabl獲取路徑中的參數(shù)
@RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET) public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) { System.out.println(id); System.out.println(name); model.addAttribute("message", "111111"); return "users"; }
例如,訪問user/123/lei路徑時,執(zhí)行以上方法,其中,參數(shù)id=123,name=lei
二、@ModelAttribute獲取POST請求的FORM表單數(shù)據(jù)
JSP表單如下
<form method="post" action="hao.do"> a: <input id="a" type="text" name="a"/> b: <input id="b" type="text" name="b"/> <input type="submit" value="Submit" /> </form>
Java Pojo如下
public class Pojo{ private String a; private int b; }
Java Controller如下
@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pojo") Pojo pojo) { return "helloWorld"; }
三、直接用HttpServletRequest獲取
@RequestMapping(method = RequestMethod.GET) public String get(HttpServletRequest request, HttpServletResponse response) { System.out.println(request.getParameter("a")); return "helloWorld"; }
四、用注解@RequestParam綁定請求參數(shù)
用注解@RequestParam綁定請求參數(shù)a到變量a。
當(dāng)請求參數(shù)a不存在時會有異常發(fā)生,可以通過設(shè)置屬性required=false解決,
例如: @RequestParam(value="a", required=false)
Controller如下
@RequestMapping(value = "/requestParam", method = RequestMethod.GET) public String setupForm(@RequestParam("a") String a, ModelMap model) { System.out.println(a); return "helloWorld"; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享名稱:Spring3MVC請求參數(shù)獲取的幾種方法小結(jié)
轉(zhuǎn)載來源:http://www.chinadenli.net/article6/jcodig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)站設(shè)計、微信公眾號、定制開發(fā)、標(biāo)簽優(yōu)化、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)