`
zzc1684
  • 浏览: 1195540 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

SPRING MVC3.2案例讲解---如何获取请求参数

阅读更多

SPRING MVC获取request参数方法:

@RequestParam   /data/param?foo=bar 等价于 request.getParameter()

 

@PathVariable  获取URL路径 // http://127.0.0.1:8010/data/path/foo 

 

@MatrixVariable 获取URL路径中协调的键值对参数

 

@RequestHeader 获取RequestHeader 中的信息

 

@RequestBody 获取request中 流信息

 

HttpEntity<String> 可以获取request中 流信息及getHeaders等信息

 

另外:编程时大都是使用表单提交request中的参数,很少设计二进制流的request提交;演示案例中使用AJAX方法来进行二进制流的提交,见JS代码;注意data属性,它是字符串而不是JSON对象,这里在HTTP传输时使用的是字符流

 

Js代码  收藏代码
  1. $("form.textForm").submit(function(event) {  
  2.         var form = $(this);  
  3.         var button = form.children(":first");  
  4.         $.ajax({ type: "POST", url: form.attr("action"),   
  5.   
  6. data: "foo", contentType: "text/plain", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});  
  7.         return false;  
  8.     });  

 

 

 

见代码:

Java代码  收藏代码
  1. @Controller  
  2. @RequestMapping("/data")  
  3. public class RequestDataController {  
  4.     // /data/param?foo=bar  
  5.     @RequestMapping(value = "param", method = RequestMethod.GET)  
  6.     public @ResponseBody  
  7.     String withParam(@RequestParam String foo) {  
  8.         return "Obtained 'foo' query parameter value '" + foo + "'";  
  9.     }  
  10.   
  11.     // http://127.0.0.1:8010/data/group?param1=foo&param2=bar&param3=baz  
  12.     @RequestMapping(value = "group", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)  
  13.     public @ResponseBody  
  14.     String withParamGroup(JavaBean bean) {  
  15.         return "Obtained parameter group " + bean;  
  16.     }  
  17.   
  18.     @RequestMapping(value = "produces", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)  
  19.     public @ResponseBody  
  20.     JavaBean byProducesJson(JavaBean bean) {  
  21.         return bean;  
  22.     }  
  23.   
  24.     // http://127.0.0.1:8010/data/path/foo  
  25.     @RequestMapping(value = "path/{var}", method = RequestMethod.GET)  
  26.     public @ResponseBody  
  27.     String withPathVariable(@PathVariable String var) {  
  28.         return "Obtained 'var' path variable value '" + var + "'";  
  29.     }  
  30.   
  31.     // http://127.0.0.1:8010/data/matrixvars;foo=bar/simple  
  32.       
  33.     // OUTPUT :Obtained matrix variable 'foo=bar' from path segment 'matrixvars'  
  34.     @RequestMapping(value = "{path}/simple", method = RequestMethod.GET)  
  35.     public @ResponseBody  
  36.     String withMatrixVariable(@PathVariable String path,  
  37.             @MatrixVariable String foo) {  
  38.         return "Obtained matrix variable 'foo=" + foo + "' from path segment '"  
  39.                 + path + "'";  
  40.     }  
  41.       
  42.     //http://127.0.0.1:8010/data/matrixvars;foo=bar1/multiple;foo=bar2  
  43.     //output: Obtained matrix variable foo=bar1 from path segment 'matrixvars' and  
  44.     // variable 'foo=bar2 from path segment 'multiple'  
  45.     @RequestMapping(value = "{path1}/{path2}", method = RequestMethod.GET)  
  46.     public @ResponseBody  
  47.     String withMatrixVariablesMultiple(@PathVariable String path1,  
  48.             @MatrixVariable(value = "foo", pathVar = "path1") String foo1,  
  49.             @PathVariable String path2,  
  50.             @MatrixVariable(value = "foo", pathVar = "path2") String foo2) {  
  51.   
  52.         return "Obtained matrix variable foo=" + foo1 + " from path segment '"  
  53.                 + path1 + "' and variable 'foo=" + foo2  
  54.                 + " from path segment '" + path2 + "'";  
  55.     }  
  56.   
  57.     // http://127.0.0.1:8010/data/header  
  58.     @RequestMapping(value = "header", method = RequestMethod.GET)  
  59.     public @ResponseBody  
  60.     String withHeader(@RequestHeader String Accept) {  
  61.         return "Obtained 'Accept' header '" + Accept + "'";  
  62.     }  
  63.   
  64.     @RequestMapping(value = "cookie", method = RequestMethod.GET)  
  65.     public @ResponseBody  
  66.     String withCookie(@CookieValue String openid_provider) {  
  67.         return "Obtained 'openid_provider' cookie '" + openid_provider + "'";  
  68.     }  
  69.   
  70.     @RequestMapping(value = "body", method = RequestMethod.POST)  
  71.     public @ResponseBody  
  72.     String withBody(@RequestBody String body) {  
  73.         return "Posted request body '" + body + "'";  
  74.     }  
  75.   
  76.     @RequestMapping(value = "entity", method = RequestMethod.POST)  
  77.     public @ResponseBody  
  78.     String withEntity(HttpEntity<String> entity) {  
  79.         return "Posted request body '" + entity.getBody() + "'; headers = "  
  80.                 + entity.getHeaders();  
  81.     }  
  82.   
  83. }  

 

 

另外还有传统的获取方式直接在 controller方法中使用HttpServletRequest request ,HttpServletResponse response,HttpSession session ;springMVC就可以直接调用这些变量中保存的内容;

见代码:

Java代码  收藏代码
  1. @Controller  
  2. public class StandardArgumentsController {  
  3.   
  4.     // request related  
  5.       
  6.     @RequestMapping(value="/data/standard/request", method=RequestMethod.GET)  
  7.     public @ResponseBody String standardRequestArgs(HttpServletRequest request, Principal user, Locale locale) {  
  8.         StringBuilder buffer = new StringBuilder();  
  9.         buffer.append("request = ").append(request).append(", ");  
  10.         buffer.append("userPrincipal = ").append(user).append(", ");  
  11.         buffer.append("requestLocale = ").append(locale);  
  12.         return buffer.toString();  
  13.     }  
  14.   
  15.     @RequestMapping(value="/data/standard/request/reader", method=RequestMethod.POST)  
  16.     public @ResponseBody String requestReader(Reader requestBodyReader) throws IOException {  
  17.         return "Read char request body = " + FileCopyUtils.copyToString(requestBodyReader);  
  18.     }  
  19.   
  20.     @RequestMapping(value="/data/standard/request/is", method=RequestMethod.POST)  
  21.     public @ResponseBody String requestReader(InputStream requestBodyIs) throws IOException {  
  22.         return "Read binary request body = " + new String(FileCopyUtils.copyToByteArray(requestBodyIs));  
  23.     }  
  24.       
  25.     // response related  
  26.   
  27.     @RequestMapping("/data/standard/response")  
  28.     public @ResponseBody String response(HttpServletResponse response) {  
  29.         return "response = " + response;  
  30.     }  
  31.   
  32.     @RequestMapping("/data/standard/response/writer")  
  33.     public void availableStandardResponseArguments(Writer responseWriter) throws IOException {  
  34.         responseWriter.write("Wrote char response using Writer");  
  35.     }  
  36.       
  37.     @RequestMapping("/data/standard/response/os")  
  38.     public void availableStandardResponseArguments(OutputStream os) throws IOException {  
  39.         os.write("Wrote binary response using OutputStream".getBytes());  
  40.     }  
  41.       
  42.     // HttpSession  
  43.   
  44.     @RequestMapping("/data/standard/session")  
  45.     public @ResponseBody String session(HttpSession session) {  
  46.         StringBuilder buffer = new StringBuilder();  
  47.         buffer.append("session=").append(session);  
  48.         return buffer.toString();  
  49.     }  
  50. }  

 

最后上传界面截图:

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics