首页 > 代码库 > SpringMVC 控制器默认支持GET和POST两种方式
SpringMVC 控制器默认支持GET和POST两种方式
在SpringMVC的controller中,@RequestMapping只写路径,不包含RequetMethod.GET和RequetMethod.POST,HttpServletRequest的getParameter(“*”)方法同时支持这两种方式。
1 @RequestMapping("savedata") 2 public @ResponseBody void savedata(HttpServletRequest req, HttpServletResponse resp) throws Exception { 3 4 String taskClass = req.getParameter("taskclass");
ServletRequest接口中对该方法的说明如下,可以从请求字符串(URL)和POST实体(通过getInputStream和getReader方法)中读取参数:
1 /** 2 * Returns the value of a request parameter as a <code>String</code>, 3 * or <code>null</code> if the parameter does not exist. Request parameters 4 * are extra information sent with the request. For HTTP servlets, 5 * parameters are contained in the query string or posted form data. 6 * 7 * <p>You should only use this method when you are sure the 8 * parameter has only one value. If the parameter might have 9 * more than one value, use {@link #getParameterValues}. 10 * 11 * <p>If you use this method with a multivalued 12 * parameter, the value returned is equal to the first value 13 * in the array returned by <code>getParameterValues</code>. 14 * 15 * <p>If the parameter data was sent in the request body, such as occurs 16 * with an HTTP POST request, then reading the body directly via {@link 17 * #getInputStream} or {@link #getReader} can interfere 18 * with the execution of this method. 19 * 20 * @param name a <code>String</code> specifying the name of the parameter 21 * 22 * @return a <code>String</code> representing the single value of 23 * the parameter 24 * 25 * @see #getParameterValues 26 */ 27 public String getParameter(String name);
参考:
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-requestmapping
SpringMVC 控制器默认支持GET和POST两种方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。