Spring MVC接收請(qǐng)求的參數(shù)
Springmvc中,接收頁面提交的數(shù)據(jù)是通過方法形參來接收:
-
處理器適配器調(diào)用springmvc使用反射將前端提交的參數(shù)傳遞給controller方法的形參
-
springmvc接收的參數(shù)都是String類型,所以spirngmvc提供了很多converter(轉(zhuǎn)換器)在特殊情況下需要自定義converter,如對(duì)日期數(shù)據(jù)
1.少量參數(shù)(基本數(shù)據(jù)類型)
直接接收即可
案例:
controller
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("/findAccount5")
public String findAccount5(String username,Model model){
model.addAttribute("msg", username);
return "success";
}
@RequestMapping("/findAccount6")
public String findAccount6(String username,Integer age,Model model){
model.addAttribute("msg", username+" "+age);
return "success";
}
}
jsp中的超鏈接
<a href="/account/findAccount5?username=eric">參數(shù)接收-基本數(shù)據(jù)類型</a>
<a href="/account/findAccount6?username=eric&age=22">參數(shù)接收-多個(gè)基本數(shù)據(jù)類型</a>
2. POJO類型參數(shù)綁定
-
編寫pojo
public class Account implements Serializable { private Integer id; private String name; private Float money; private Address address; //省略get set toString方法 }
public class Address implements Serializable { private String provinceName; private String cityName; //省略get set toString方法 }
-
編寫controller
package com.by.controller; import com.by.pojo.Account; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/account") public class AccountController { @RequestMapping("/saveAccount") public String saveAccount(Account account, Model model){ model.addAttribute("msg", account); return "success"; } }
-
在index.jsp里面添加表單
<form action="account/saveAccount" method="post"> 賬戶名稱:<input type="text" name="name"><br/> 賬戶金額:<input type="text" name="money"><br/> 賬戶省份:<input type="text" name="address.provinceName"><br/> 賬戶城市:<input type="text" name="address.cityName"><br/> <input type="submit" value="保存"> </form>
3. restful
-
restful概述:
(Representational State Transfer,表現(xiàn)層狀態(tài)轉(zhuǎn)移):URL定位資源時(shí),用HTTP動(dòng)詞(GET,POST,DELETE,PUT)描述操作
restful風(fēng)格URL
-
在Restful之前的操作:
http://127.0.0.1/user/query?id=1 根據(jù)用戶id查詢用戶數(shù)據(jù)
http://127.0.0.1/user/save 新增用戶
http://127.0.0.1/user/update?id=1 修改用戶信息
http://127.0.0.1/user/delete?id=1 刪除用戶信息 -
RESTful用法:
http://127.0.0.1/user/1 GET 根據(jù)用戶id查詢用戶數(shù)據(jù)
http://127.0.0.1/user POST 新增用戶
http://127.0.0.1/user PUT 修改用戶信息
http://127.0.0.1/user/1 DELETE 刪除用戶信息 -
RESTful總結(jié):
Restful風(fēng)格就是請(qǐng)求url統(tǒng)一,根據(jù)不同的請(qǐng)求方式,請(qǐng)求不同的后臺(tái)方法。如果需要攜帶參數(shù),在url上使用/{}占位符。
@PathVaribale
-
作用
用于綁定url中的占位符。例如:/account/{id},這個(gè){id}就是url占位符
url支持占位符是spring3.0之后加入的,是springmvc支持rest風(fēng)格url的重要標(biāo)志。
-
描述需要使用指定的請(qǐng)求方式來請(qǐng)求該方法
@Controller @RequestMapping("/account") public class AccountController { @RequestMapping(value="/findAccount7/{id}") public String findAccount11(@PathVariable Integer id, Model model){ model.addAttribute("msg", id); return "success"; } }
-
測(cè)試:在index.jsp里面定義超鏈接
<a href="/account/findAccount7/123">restful傳參</a><br>
4.JSON接收
jsp文章來源:http://www.zghlxwxcb.cn/news/detail-808196.html
$.ajax({
type:"post",
url:"/account/findAccount",
contentType:"application/json;charset=UTF-8",
data:'{"id":1,"name":"張二狗","money":999.0}',
success:function(data){
//{id:2, name:鐵柱, money:100}
alert(data.name);
}
})
Controller文章來源地址http://www.zghlxwxcb.cn/news/detail-808196.html
@RequestMapping("/findAccount")
@ResponseBody
public Account findAccount(@RequestBody Account account){
return account;
}
到了這里,關(guān)于Spring MVC學(xué)習(xí)之——如何接收請(qǐng)求傳過來的參數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!