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 ...
随机推荐
- TZ_10_spring-sucrity 服务器和页面的权限控制
1.在服务器端我们可以通过Spring security提供的注解对方法来进行权限控制. Spring Security在方法的权限控制上支持三种类型的注解,JSR-250注解.@Secured注解和 ...
- JavaScript多继承(转载)
js里是否有多继承,如何实现多继承.在这里可以看看java是如何处理多继承的问题,java里是没有多继承的,即一个子类不能同时继承多个父类,但可以实现多个接口,这也间接的实现了多继承.主要是因为多继承 ...
- zabbix自定义监控redis
zabbix监控redis脚本 #!/bin/bash #此脚本用来获取redis-cli info信息 redis_cli="/usr/local/redis/bin/redis-cli& ...
- 外观模式(Facade)(门面模式、子系统容易使用)
外观(Facade)模式的定义:是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式.该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低 ...
- 【同余最短路】【例题集合】洛谷P3403 跳楼机/P2371 墨墨的等式
接触到的新内容,[同余最短路]. 代码很好写,但思路不好理解. 同余最短路,并不是用同余来跑最短路,而是通过同余来构造某些状态,从而达到优化时间空间复杂度的目的.往往这些状态就是最短路中的点,可以类比 ...
- POJ4852 Ants
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20047 Accepted: 8330 Description ...
- django 结合 bootstrap 使用
git clone https://github.com/dyve/django-bootstrap3.git 要运行demo,需要在demo 中为其增加一个符号链接 bootstrap3 到上层目录 ...
- python实现六大分群质量评估指标(兰德系数、互信息、轮廓系数)
python实现六大分群质量评估指标(兰德系数.互信息.轮廓系数) 1 R语言中的分群质量--轮廓系数 因为先前惯用R语言,那么来看看R语言中的分群质量评估,节选自笔记︱多种常见聚类模型以及分群质量评 ...
- nested exception is org.hibernate.MappingException解决方案
1.可能是因为映射文件( Order.hbm.xm)配置的class路径出错 <hibernate-mapping> <class name="com.web.bean.O ...
- Linux下下载安装jdk1.7和IDEA
一.安装JDK1.7 准备: 到Oracle官网下载下载jdk1.7,参考博客 https://blog.csdn.net/H12KJGJ/article/details/79240984 官网地址: ...