spring mvc请求方式限定以及返回值
1、@RequestMapping(method = RequestMethod.GET)如果通过Post访问则报错:HTTP Status 405 - Request method 'POST' not supported例如:@RequestMapping(value="/editItem",method=RequestMethod.GET)
2、限定POST方法@RequestMapping(method = RequestMethod.POST)如果通过Post访问则报错:HTTP Status 405 - Request method 'GET' not supported
3、都可以;@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})controller方法返回值1.1返回ModelAndViewcontroller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。
4、在controller方法形参上可以定义request和response,使用re孥恶膈茯quest或response指定响应结果:1、使用request转向页面,如下:request.getRequestDispatcher("页面路径").forward(request, response);2、也可以通过response页面重定向:response.sendRedirect("url")
5、也可以通过response指定响应结果,例如响应json数据如下:response.setCharacterEncoding("utf-8");response.setContentType("application/json;charset=utf-8");response.getWriter().write("json串");
6、1.1返回字符串controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地记醋弭床址。//指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/item/editItem.jspreturn"item/editItem";//重定向到queryItem.action地址,request无法带过去return"redirect:queryItem.action";
7、redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。由于新发起一个request原来的参数在转发时就不能传递到下一个url,如果要传参数可以/item/queryItem.action后边加参数,如下:/item/queryItem?...&…..