@RequestParam 綁定單個(gè)請求
RequestMapping(value = "/form2")
@ResponseBody
public String form2(@RequestParam String name){
logger.info("name=" + name );
return "";
}
@RequestParam
有required(是否必填),defaultValue(默認(rèn)參數(shù))參數(shù)
默認(rèn)required=true,defaultValue為空
如果name參數(shù)不存在則返回400
使用下面這種寫法則required默認(rèn)為false
@RequestMapping(value = "/form")
@ResponseBody
public String form(String name, Integer age) {
logger.info("name=" + name + "," + "age=" + age);
return "";
}
@RequestParam
還支持?jǐn)?shù)組形式
@RequestMapping(value = "/names")
@ResponseBody
public String names(@RequestParam String[] name) {
logger.info("name=" + Arrays.asList(name));
return Arrays.asList(name).toString();
}
對應(yīng)請求/names.do?name=張三,李四
或者/names.do?name=張三&name=李四
@PathVariable 綁定uri變量值
@RequestMapping("/user/{id}")
@ResponseBody
public String getUser(@PathVariable Long id) {
return String.valueOf(id);
}
請求/user/1返回id為1
@CookieValue 綁定cookie的值
@RequestMapping("/getCookie")
@ResponseBody
public String getCookie(@CookieValue("JSESSIONID") String sessionId) {
return sessionId;
}
@ModelAttribute綁定參數(shù)到命令對象
- 綁定請求參數(shù)到對象
先定義兩個(gè)對象
Role
public class Role implements Serializable {
private String id;
}
User
public class User implements Serializable {
private String id;
private Role role;
}
@RequestMapping("/getUserByModel")
@ResponseBody
public String getUserByModel(@ModelAttribute("user") User user) {
logger.info(user.toString());
return "";
}
對應(yīng)請求/getUserByModel.do?id=123&role.id=1
- 暴露表單引用對象為模型數(shù)據(jù)
@ModelAttribute("user")
public User getUser(String userId) {
User user = new User();
user.setId("11");
Role role=new Role();
role.setId("123");
user.setRole(role);
return user;
}
@RequestMapping("/getUserModel")
@ResponseBody
public String getUserModel(@ModelAttribute User user) {
return user.toString();
}
請求/getUserModel?id=1
返回 User{id='1', role=Role{id='123'}}
使用@ModelAttribute
注釋的方法會在所有Controller方法前運(yùn)行
因?yàn)橄冗\(yùn)行了@ModelAttribute
注解的方法,生成了一個(gè)user對象,之后user對象的id屬性被修改為1
@SessionAttributes綁定命令對象到session
springmvc提供了@SessionAttributes
用來對會話數(shù)據(jù)的存取
添加session
@RequestMapping("/setSession")
@ResponseBody
public String setSession(
HttpSession session) {
City city = new City();
city.setCityName("shanghai");
session.setAttribute("city", city);
return city.toString();
}
獲取session數(shù)據(jù)
//在類頭部添加需要的session屬性
@Controller
@SessionAttributes(value = {"city"})
public class TestController {
@RequestMapping("/getSession")
@ResponseBody
public String getSession(@ModelAttribute City city) {
return city.toString();
}
}
清除本次會話session
@RequestMapping("/clearSession")
@ResponseBody
public String clearSession(SessionStatus status) {
status.setComplete();
return "success";
}
@RequestBody綁定請求的內(nèi)容區(qū)數(shù)據(jù)并能進(jìn)行自動類型轉(zhuǎn)換等
使用RequestBody可以將json轉(zhuǎn)化為對象
@RequestMapping("/getUserByJson")
@ResponseBody
public String getUserByJson(@RequestBody User user){
return user.toString();
}
發(fā)送json格式報(bào)文
{
"id":"123"
}
返回結(jié)果User{id='123', role=null}
@RequestPart綁定“multipart/data”數(shù)據(jù)
@RequestMapping("/getFile")
@ResponseBody
public String getFile(@RequestPart("image") MultipartFile file){
System.out.println(file.getName()+","+file.getSize());
return "success";
}