spingMvc REST的风格:
/user_show?id=120
/user/120
/user_delete?id=123
/user/123/delete
/user_updateInput?id=123
/user/123/update
/user_list
/users
/user/users
REST的风格不等于使用了REST技术
@PathVariable路径变量参数的使用 :value="/{username}" 路径参数一定要使用{}包裹起来;
@RequestMapping(value="/{username}",method=RequestMethod.GET)
public String show(@PathVariable String username,Model model) {
model.addAttribute(users.get(username));
return "user/show";
}
@RequestMapping(value="/{username}",method=RequestMethod.GET,params="json")
@ResponseBody
public User show(@PathVariable String username) {
return users.get(username);
}
@RequestMapping(value="/{username}/update",method=RequestMethod.GET)
public String update(@PathVariable String username,Model model) {
model.addAttribute(users.get(username));
return "user/update";
}
@RequestMapping(value="/{username}/update",method=RequestMethod.POST)
public String update(@PathVariable String username,@Validated User user,BindingResult br) {
if(br.hasErrors()) {
//如果有错误直接跳转到add视图
return "user/update";
}
users.put(username, user);
return "redirect:/user/users";
}
@RequestMapping(value="/{username}/delete",method=RequestMethod.GET)
public String delete(@PathVariable String username) {
users.remove(username);
return "redirect:/user/users";
}