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>
<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";
- }
@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
- }
public enum RequestMethod {
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}
- RequestMethod[] method() default {};
RequestMethod[] method() default {};
SpringMVC method属性与http请求方法一致的更多相关文章
- SpringMVC学习 -- 使用 @RequestMapping 映射请求
在控制器的类定义及方法出定义出都可以标注 @RequestMapping: 类定义处:提供初步的请求映射信息.相对于 Web 应用的根目录. 方法定义出:提供进一步的细分映射信息.相对于类定义处的 U ...
- [转]Struts2理解--动态方法和method属性及通配符_默认Action
众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...
- Struts2理解--动态方法和method属性及通配符_默认Action
众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...
- SpringMVC 学习笔记(请求方法的返回值和参数)
在用注解对配置 处理器时,一般是一个方法处理一个请求,不同方法的返回类型有着不同的意义. 返回值为 ModelAndView 类型 ModelAndView 是Model 和 View 的一个集合类型 ...
- SpringMVC04 很杂很重要(注解,乱码处理,通配符,域属性调用,校正参数名称,访问路径,请求、响应携带参数,请求方法)
1.导入架包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...
- springMVC学习总结(二)路径映射和请求方法限定
springMVC学习总结(二)路径映射和请求方法限定 一.路径映射 无参数的访问路径 对springmvc项目的访问路径,是由根路径和子路径组成:在注解式开发中,根路径标注在类名之上,子路径标注在方 ...
- springMVC源码分析--DispatcherServlet请求获取及处理
在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...
- SpringMVC之使用 @RequestMapping 映射请求
@RequestMapping注解 SpringMVC使用该注解让控制器知道可以处理哪些请求路径的,除了可以修饰方法,还可以修饰在类上. – 类定义处:提供初步的请求映射信息.相对于 WEB 应用的根 ...
- springmvc 发送PUT 和 DELETE 请求
一: 发送 DELETE 或者 PUT 请求: 1.在表单中加入一个隐藏的参数: _method , 值是 DELETE (或者PUT) <form action="springmv ...
随机推荐
- Liferay 7:Liferay DXP解决方案
分享是美德,欢迎探讨技术 这个作者很厉害呀,写的博客都是解决很刁钻问题的.强烈推荐 http://www.liferaysolution.com/2017/06/captcha-recaptcha-w ...
- FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)
自从继承了hibernate ,全都是些奇葩问题. 努力解决中,先发布,以备忘
- eclipse 代码格式化快捷键CTRL SHIFT F无反应的解决办法
基本上就是被其他软件占用了,比如输入法的简繁体切换,改其他软件,保留eclipse就好
- SQLite in Python: 如何在Python中使用SQLite数据库
SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 249 描述的 DB-API 2.0 规 ...
- 【P1203】 【USACO1.1】坏掉的项链Broken Necklace
P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 ...
- php分页查询的简单实现代码
<body><h1>分页查询</h1><?phpinclude("DADB.class.php");$db=new DADB();$tj= ...
- 错觉-Info:让你难以置信的视错觉
ylbtech-错觉-Info:让你难以置信的视错觉 1.返回顶部 1. 看下图:如果你看到舞者逆时针旋转说明你用左脑,如果看到顺时针旋转说明你用右脑思维. 据说这是耶鲁大学五年的研究成果. 下图 ...
- Watering Grass (贪心,最小覆盖)
参考: https://blog.csdn.net/shuangde800/article/details/7828675 https://www.cnblogs.com/haoabcd2010/p/ ...
- 机器学习之Adaboost算法原理
转自:http://www.cnblogs.com/pinard/p/6133937.html 在集成学习原理小结中,我们讲到了集成学习按照个体学习器之间是否存在依赖关系可以分为两类,第一个是个体学习 ...
- 简单介绍几个CSSReset的方法
对于小型的网站来说,用这个并不会带来大的资源浪费,但如果是像Yahoo这种架构非常大的网站,必须要有选择地进行CSS重设,以减 少资源浪费. 正在使用CSS的你,用过CSS Reset吗?当然,或许你 ...