SpringMVC method属性与http请求方法一致

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

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

表单如下:

<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 注解如下:

@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)&&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请求方法

public enum RequestMethod {

	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE

}
RequestMethod[] method() default {};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值