【spring】springmvc接收请求参数的方法
- - 开源软件 - ITeye博客 控制器的请求处理方法的签名可以将任何事物作为参数,我们可以将HttpServletRequest、HttpServletResponse、 String或者数字参数传递进来,这些参数可以对应请求中的查询参数、cookie值、HTTP请求头的值或其他一些可能的选项. 使用@RequestParam绑定请求参数值.
@RequestMapping(value="/method1") public String method1( @RequestParam(value="userName",required = false) String userName, @RequestParam("pwd") String pwd){ return "methodSignature"; }
@RequestMapping(value="/method2") public String method2( @CookieValue(value="sessionId",required=false)String sessionId){ return "methodSignature"; }
@RequestMapping(value="/method3") public String method3( @RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Keep-Alive") long keepAlive){ return "methodSignature"; }
public ModelAndView helloWorld( @PathVariable String id, @PathVariable String str) { System.out.println(id); System.out.println(str); return new ModelAndView( " /helloWorld " ); }
@RequestMapping(value="/method4") public String method4(User user){ return "methodSignature"; }
@RequestMapping(value="method5") public void method5( HttpServletRequest request, HttpServletResponse response, HttpSession session){ String userName = WebUtils.findParameterValue(request, "userName"); response.addCookie(new Cookie("userName",userName)); }
@RequestMapping(value="method6") public String method6(WebRequest request){ String userName = request.getParameter("userName"); return "methodSignature"; }
@RequestMapping(value="method7") public void method7(OutputStream os) throws IOException{ Resource res = new ClassPathResource("/image.jpg");//读取类路径下的图片文件 FileCopyUtils.copy(res.getInputStream(), os); }
@RequestMapping(value="method8") public String method8(Locale locale,Principal principal){ return "methodSignature"; }