在springMVC中,@requestMapping注解有method属性,在没有指定method的值时,默认映射所有http请求方法,如果仅想接收一种请求方法,需用method=RequestMethod.GET

或其他 请求方法指定,如果提交表单时form标签中method的请求方法与requestMapping中指定的不同,则会报错。如:

表单如下:

  1. <form id="frLogin" name="frLogin" method="post"  action="./loginmvc/login">
  2. <table>
  3. <tr>
  4. <td>姓名</td>
  5. <td><input type="text" name="txtName" id="txtName"/></td>
  6. </tr>
  7. <tr>
  8. <td>姓名</td>
  9. <td><input type="password" name="pwd" id="pwd"/></td>
  10. </tr>
  11. <tr>
  12. <td align="right"><input type="submit" value="登录"/></td>
  13. <td><input type="reset" value="重填"/></td>
  14. </tr>
  15. </table>
  16. </form>
<form id="frLogin" name="frLogin" method="post"  action="./loginmvc/login">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="txtName" id="txtName"/></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="password" name="pwd" id="pwd"/></td>
</tr>
<tr>
<td align="right"><input type="submit" value="登录"/></td>
<td><input type="reset" value="重填"/></td>
</tr>
</table>
</form>

springmvc requestMapping 注解如下:


  1. @RequestMapping(value="/login",method=RequestMethod.GET)
  2. public String login(HttpServletRequest request,HttpServletResponse response){
  3. String strName=request.getParameter("txtName");
  4. String strPassword=request.getParameter("pwd");
  5. String sResult="loginError";
  6. if(StringUtils.isBlank(strName)&&StringUtils.isBlank(strPassword)){
  7. sResult="loginOK";
  8. }
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(HttpServletRequest request,HttpServletResponse response){
String strName=request.getParameter("txtName");
String strPassword=request.getParameter("pwd");
String sResult="loginError";
	if(StringUtils.isBlank(strName)&amp;&amp;StringUtils.isBlank(strPassword)){
sResult="loginOK";
}


浏览器报错为:

HTTP Status 405 - Request method 'POST' not supported


type Status report

message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource.


查network,请求体说明:

因此在建立映射时,应当注意http请求方法与requestMapping注解一致,或者在注解中不再指定method,而是默认通过枚举自动映射所有http请求方法

  1. public enum RequestMethod {
  2. GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
  3. }
public enum RequestMethod {
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE

}

  1. RequestMethod[] method() default {};  
RequestMethod[] method() default {};

SpringMVC method属性与http请求方法一致的更多相关文章

  1. SpringMVC学习 -- 使用 @RequestMapping 映射请求

    在控制器的类定义及方法出定义出都可以标注 @RequestMapping: 类定义处:提供初步的请求映射信息.相对于 Web 应用的根目录. 方法定义出:提供进一步的细分映射信息.相对于类定义处的 U ...

  2. [转]Struts2理解--动态方法和method属性及通配符_默认Action

    众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...

  3. Struts2理解--动态方法和method属性及通配符_默认Action

    众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...

  4. SpringMVC 学习笔记(请求方法的返回值和参数)

    在用注解对配置 处理器时,一般是一个方法处理一个请求,不同方法的返回类型有着不同的意义. 返回值为 ModelAndView 类型 ModelAndView 是Model 和 View 的一个集合类型 ...

  5. SpringMVC04 很杂很重要(注解,乱码处理,通配符,域属性调用,校正参数名称,访问路径,请求、响应携带参数,请求方法)

    1.导入架包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...

  6. springMVC学习总结(二)路径映射和请求方法限定

    springMVC学习总结(二)路径映射和请求方法限定 一.路径映射 无参数的访问路径 对springmvc项目的访问路径,是由根路径和子路径组成:在注解式开发中,根路径标注在类名之上,子路径标注在方 ...

  7. springMVC源码分析--DispatcherServlet请求获取及处理

    在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...

  8. SpringMVC之使用 @RequestMapping 映射请求

    @RequestMapping注解 SpringMVC使用该注解让控制器知道可以处理哪些请求路径的,除了可以修饰方法,还可以修饰在类上. – 类定义处:提供初步的请求映射信息.相对于 WEB 应用的根 ...

  9. springmvc 发送PUT 和 DELETE 请求

    一: 发送 DELETE 或者 PUT 请求: 1.在表单中加入一个隐藏的参数: _method  , 值是 DELETE (或者PUT) <form action="springmv ...

随机推荐

  1. 【html、CSS、javascript-1】html基础

    HTML   翻译成代码如下: web: import socket def handle_request(client): buf = client.recv(1024) client.sendal ...

  2. Vue-cli3.x在开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chunk {/d} failed.或者Uncaught SyntaxError: Unexpected token <错误

    使用Vue-cli3.x开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chu ...

  3. WPF学习笔记-用Expression Blend制作自定义按钮

    1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a cop ...

  4. 关于python的列表操作(二):排序,统计

    # 列表操作 num_list = [2, 5, 8, 6, 7, 9, 5, 7] # 升序 num_list.sort() print(num_list) # 降序 num_list.sort(r ...

  5. 模板与泛型编程 c++ primer ch16.1

    在摸板定义中,模板参数列表不能为空, 编译器用推断出的参数来进行 实例化(instantiation) 一般来说 模板是有type parameter 但是也可以声明 nontype paramete ...

  6. 2019.9.16 csp-s模拟测试44 反思总结

    虽然说好像没有什么写这个的价值OAO 来了来了来写总结了,不能怨任何东西,就是自己垃圾x 开题顺序又和主流背道而驰,先一头扎进了公认最迷的T2,瞎搞两个小时头铁出来,然后T1和T3爆炸.基础很差,全靠 ...

  7. php分页查询的简单实现代码

    <body><h1>分页查询</h1><?phpinclude("DADB.class.php");$db=new DADB();$tj= ...

  8. Django用户名密码错误提示

    from django.shortcuts import render # Create your views here. from django.shortcuts import render fr ...

  9. 提交方式get和post有什么区别

    提交方式post和get有什么区别? (1)post是向服务器传送数据:get是从服务器上获取数据. (2)在客户端,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个 ...

  10. 安装 cx_Oracle

    1.下载 oracle client instant  和  sdk,  全部解压到 /opt/instantclient_11_2/ http://www.oracle.com/technetwor ...