`

spring_mvc(三)Obtaining Request Data

 
阅读更多
Obtained 'foo' query parameter value 'bar'

http://localhost:8080/spring_mvc_test/data/param?foo=test
@RequestMapping(value="param")
public @ResponseBody String withParam(@RequestParam String foo) {
      String message = "Obtained 'foo' query parameter value '" + foo + "'";
      return message;
}

结果:Obtained 'foo' query parameter value 'test'

Obtain parameter group Param1:foo, Param2:bar, Params:baz

http://localhost:8080/spring_mvc_test/data/group?param1=foo&param2=bar&param3=baz
@RequestMapping(value="group")
public @ResponseBody String withParamGroup(JavaBean bean) {
         String message = "Obtained parameter group: " 
	+ bean.getParam1() + "," + bean.getParam2() + "," +  bean.getParam3();
         return message;
}

JavaBean:
private String param1;	
private String param2;
private String param3;


Obtain 'var' path variable value 'foo'

http://localhost:8080/spring_mvc_test/data/path/foo
@RequestMapping(value="path/{var}")
public @ResponseBody String withPathVariable(@PathVariable String var) {
	String message = "path variable:" + var;
	return message;
}


Obtained 'Accept'header 'text/plain, */*'
http://localhost:8080/spring_mvc_test/data/header
@RequestMapping(value="header")
public @ResponseBody String withHeader(@RequestHeader String Accept) {
	String message = "Obtained 'Accept' header '" + Accept + "'";
	return message;
}

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, */*'

posted request body 'foo'
<form action="http://localhost:8080/spring_mvc_test/data/body" method="post">
	<input type="text" name="aaa"/>
	<input type="submit" value="提交" />
</form>

@RequestMapping(value="body", method=RequestMethod.POST)
public @ResponseBody String withBody(@RequestBody String body) {
	String message = "body:" + body;
	return message;
}

结果:body:aaa=aaasdfasf

Request Body and Headers
@RequestMapping(value="entity", method=RequestMethod.POST)
public @ResponseBody String withEntity(HttpEntity<String> entity) {
	String message = "Posted request body '" + entity.getBody() "'; header = " + entity.getHeaders();
	return message;
}


read a form
<form action="http://localhost:8080/spring_mvc_test/data/form" method="post">
	<input type="text" id="aaa" name="aaa"/>
	<input type="text" id="bbb" name="bbb"/>
	<input type="submit" value="提交" />
</form>

@RequestMapping(value="form", method=RequestMethod.POST)
public @ResponseBody String withEntity(@RequestBody MultiValueMap<String, String> form) {
String message = "read from map" + form;
return message;
}
read from map{aaa=[qqq], bbb=[www]}

write a form

http://localhost:8080/spring_mvc_test/data/form
@RequestMapping(value="/form", method=RequestMethod.GET)
public @ResponseBody MultiValueMap<String, String> writeForm() {
   MultiValueMap<String, String> map = new LinkedMultiValueMap<String , String>();
   map.add("foo", "bar");
   map.add("fruit", "apple");
   return map;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics