SpringMVC ways acquisition request parameters

SpringMVC way acquisition parameters:

  • Use native receiving HttpServletRequest
  • @RequestParam、@RequestHeader、@CookieValue
  • @RequestBody
  • @PathVariable
  • @SessionAttribute
  • @ModelAttribute

To get requests, the way you can use are:

  • Use native direct request request.getParameter("")

  • Use annotations @RequestParam, the form:
    value is the value of the annotation is below the "name" and "pwd" parameter to be consistent with the requested name, springmvc parameter values automatically given userName and password

@RequestMapping(value = "/getData")
public void getData(@RequestParam("name") String userName, 
                    @RequestParam("pwd") String password) {
	System.out.println(userName+","+password);
}
  • Property names declared directly in the parameter list of the method, of course, this parameter consistent with the request for a parameter name
@RequestMapping(value = "/getData")
public void getData(String name, String password) {
	System.out.println(name+","+password);
}

For the post request, the embodiment can be used are:

  • Use native request
  • Use annotations @RequestParam, usage is consistent with get, request parameters, then use a lot of this is very cumbersome
  • Use @RequestBody, all request parameters may be converted to a string, can be packaged as a set of map usage:
@RequestMapping("/getData")
private void getData(@RequestBody String strAttrs) throws IOException {
	System.out.println(strAttrs);
}
/* 这是发请求给后端的axios */
var app1 = new Vue({
	el: "#app1",
	methods: {
	getData: function () {
		axios.post("getData", {
				categoryId: 2,
           	 	field: "add_time",
            	order: "DESC",
            	start: 0,
            	limit: 5
			});
		}
	}
});

Print out the resulting string:{"categoryId":2,"field":"add_time","order":"DESC","start":0,"limit":5}

The request parameter map may encapsulate usage:

@RequestMapping("/getData")
private void getData(@RequestBody Map<String, Object> mapAttrs) throws IOException {
	System.out.println(mapAttrs);

    System.out.println(mapAttrs.get("categoryId"));
    System.out.println(mapAttrs.get("field"));
    System.out.println(mapAttrs.get("order"));
    System.out.println(mapAttrs.get("key"));
    System.out.println(mapAttrs.get("start"));
    System.out.println(mapAttrs.get("limit"));
}

Print out the results:
{categoryId=2, field=add_time, order=DESC, start=0, limit=5}
categoryId:2
field:add_time
order:DESC
key:null
start:0
limit:5

Illustrate the use of other notes:

  • @ RequestHeader, @ CookieValue @RequestParam consistent usage and usage, an acquisition request of the former head, which acquires data request Cookie
  • @PathVariable restful programming style may be suitable, the annotation can be acquired in the url parameter, like this:
/* 发给controller的删除url */
axios.delete("${pageContext.request.contextPath}/action/"+id)
@ResponseBody
@RequestMapping(value = "/action/{id}", method = RequestMethod.DELETE)
public Integer deleteAnimal(@PathVariable("id") Integer id) {

	return service.deleteAnimal(id);
}
  • @SessionAttribute, the annotation may specify the name of the corresponding property value into the data value corresponding to the type HttpSession session object, attribute types may be specified data into the session, only the modified annotation type, for example:
    @SessionAttributes(value = {"name","pwd"},types = {Integer.class})
    is named "name", "pwd" Integer data with all types of data stored in session

  • @ModelAttribute, the method may be modified annotation process parameter

  1. When the modification method: This method is called before each target method, in which you can put the target method required model data

  2. For example, when modifying the parameter:
    SpringMVC first looks for data named "userId" from the model data, if not, look at whether the class with @SessionAttribute modification, to find the data session, if you can not find it He throws found, during an assignment on

@ResponseBody
@RequestMapping("/getOneInfoById")
private User getOneInfoById(@ModelAttribute("userId") Integer userId) throws IOException {
	if (userId != null) {
		return dao.getOneInfoById(userId);
	}
	return null;
}
Published 12 original articles · won praise 3 · Views 238

Guess you like

Origin blog.csdn.net/qq_38599840/article/details/105037010