spring_mvc(三)Obtaining Request Data
[color=blue]Obtained 'foo' query parameter value 'bar'[/color] http://localhost:8080/spring_mvc_test/data/param?foo=test [code="java"]@RequestMapping(value="param") public @ResponseBody String withParam(@RequestParam String foo) { String message = "Obtained 'foo' query parameter value '" + foo + "'"; return message; }[/code] 结果:Obtained 'foo' query parameter value 'test' [color=blue]Obtain parameter group Param1:foo, Param2:bar, Params:baz[/color] http://localhost:8080/spring_mvc_test/data/group?param1=foo¶m2=bar¶m3=baz [code="java"]@RequestMapping(value="group") public @ResponseBody String withParamGroup(JavaBean bean) { String message = "Obtained parameter group: " + bean.getParam1() + "," + bean.getParam2() + "," + bean.getParam3(); return message; }[/code] JavaBean: [code="java"]private String param1; private String param2; private String param3;[/code] [color=blue]Obtain 'var' path variable value 'foo'[/color] http://localhost:8080/spring_mvc_test/data/path/foo [code="java"]@RequestMapping(value="path/{var}") public @ResponseBody String withPathVariable(@PathVariable String var) { String message = "path variable:" + var; return message; }[/code] [color=blue]Obtained 'Accept'header 'text/plain, */*'[/color] http://localhost:8080/spring_mvc_test/data/header [code="java"]@RequestMapping(value="header") public @ResponseBody String withHeader(@RequestHeader String Accept) { String message = "Obtained 'Accept' header '" + Accept + "'"; return message; }[/code] Obtained 'Accept' header 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*' [color=blue]posted request body 'foo'[/color] [code="java"]
[/code] [code="java"]@RequestMapping(value="body", method=RequestMethod.POST) public @ResponseBody String withBody(@RequestBody String body) { String message = "body:" + body; return message; }[/code] 结果:body:aaa=aaasdfasf [color=blue]Request Body and Headers[/color] [code="java"]@RequestMapping(value="entity", method=RequestMethod.POST) public @ResponseBody String withEntity(HttpEntity entity) { String message = "Posted request body '" + entity.getBody() "'; header = " + entity.getHeaders(); return message; }[/code] [color=blue]read a form[/color] [code="java"]
[/code] @RequestMapping(value="form", method=RequestMethod.POST) public @ResponseBody String withEntity(@RequestBody MultiValueMap form) { String message = "read from map" + form; return message; } read from map{aaa=[qqq], bbb=[www]} [color=blue]write a form[/color] http://localhost:8080/spring_mvc_test/data/form [code="java"]@RequestMapping(value="/form", method=RequestMethod.GET) public @ResponseBody MultiValueMap writeForm() { MultiValueMap map = new LinkedMultiValueMap(); map.add("foo", "bar"); map.add("fruit", "apple"); return map; }[/code]