SpringMVC:常用关键字

来自Wikioe
跳到导航 跳到搜索


关键字 说明 备注 链接
url映射 @RequestMapping url映射 @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
@PathVaraible 动态URI、RESTful
@RequestMapping(value = "/fetch/{id:[a-z]+}/{name} ", method = RequestMethod.GET)
public String getDynamicUriValueRegex(@PathVariable("name") String name) {}

@GetMapping RequestMapping的组合注解 =@RequestMapping(method =RequestMethod.GET)

@PostMapping@PutMapping@DeleteMapping@PatchMapping类似

参数绑定 @RequestParam 绑定request参数到方法形参 public String editItems(@RequestParam(value="id",required=true,defaultValue="10001") Integer items_id)throws Exception {}
Validation @Validated public String editItemsSubmit(Integer id,@Validated(value = { ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{}
@NotNull 校验规则 @NotNull(message="{item.price.isNull}",groups= {ValidGroup1.class})
数据回显 Model 数据回显(简单类型、pojo回显)
  1. @RequestMapping(value="/editItems",method={RequestMethod.GET})
    public String editItems(Model model,Integer id)throws Exception{
    	//传入的id重新放到request域
    	model.addAttribute("id", id);
    }
    
  2. @RequestMapping("/editItemSubmit")
    public String editItemSubmit(Model model,ItemsCustom itemsCustom){
    	// 可以直接使用model将提交pojo回显到页面
    	model.addAttribute("items", itemsCustom);
    }
    

@ModelAttribute pojo类型回显、方法返回值回显
  1. @RequestMapping("/editItemSubmit")
    public String editItemSubmit(Integer id,@ModelAttribute("item") ItemsCustom itemsCustom){}
    
  2. @ModelAttribute("itemtypes")
    public Map<String, String> getItemTypes() {
    
    	Map<String, String> itemTypes = new HashMap<String, String>();
    	itemTypes.put("101", "数码");
    	itemTypes.put("102", "母婴");
    
    	return itemTypes;
    }
    

json数据交互 @RequestBody 将请求的json串转成pojo对象,进行参数绑定。
@RequestMapping("/editItemSubmit_RequestJson")
public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) throws Exception {}
@ResponseBody 将pojo对象转成json,进行输出。