1.出错时的代码

(1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param>
​<!-- 配置HiddenHttpMethodFilter:可以把post请求转为DELETE请求和PUT请求 --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 配置DispatcherServlet --><!-- 默认的配置文件为 /WEB-INF/<servlet-name>-servlet.xml --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

(2)dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
​<!--自动扫描包中的Controlller --><context:component-scan base-package="com.itheima.controller"/><!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><!-- 前缀 --><property name="suffix" value=".jsp"/><!-- 后缀,自动拼接 --></bean>
</beans>

(4)Test.java

    @RequestMapping(value = "order", method = RequestMethod.DELETE)@ResponseBody()public String testOrderDelete(@RequestParam("id") Integer id) {System.out.println("删除id为" + id + "的员工");return "success";}
​@RequestMapping(value = "order", method = RequestMethod.PUT)@ResponseBody()public String testOrderPut(@RequestParam("id") Integer id) {System.out.println("更新id为" + id + "的员工");return "success";}

(5)index.jsp

<form action="order" method="post">id:<input type="text" name="id"><input type="hidden" name="_method" value="DELETE"><input type="submit" value="order提交delete">
</form>
​
<form action="order" method="post">id:<input type="text" name="id"><input type="hidden" name="_method" value="PUT"><input type="submit" value="order提交put">
</form>

2.错误原因分析

当在index.jsp中提交后,HiddenHttpMethodFilter会将method转换成对应的DELETE或PUT,SpingMVC会继续用对应的请求重定向到success.jsp中,而jsp只支持GET、POST、HEAD请求方法。

3.解决方法

(1)为controller里的方法加上@ResponseBody()注解,并且返回一个字符串。不过此方法无法进入指定success.jsp页面。

  @RequestMapping(value = "order", method = RequestMethod.PUT)@ResponseBody()public String testOrderPut(@RequestParam("id") Integer id) {System.out.println("更新id为" + id + "的员工");return "success";}

(2)使用重定向跳转到指定页面

  @RequestMapping(value = "order", method = RequestMethod.DELETE)public String testOrderDelete(@RequestParam("id") Integer id) {System.out.println("删除id为" + id + "的员工");//无法重定向到WEB-INF目录中,若要访问,需把该页面放入其它路径return "redirect:success.jsp";}

(3)taocat换到7.0以及以下版本

(4)在你的success页面头部文件设置其页面为错误页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>

4.HiddenHttpMethodFilter转换请求的源码

   private String methodParam = "_method";
​protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HttpServletRequest requestToUse = request;if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {String paramValue = request.getParameter(this.methodParam);if (StringUtils.hasLength(paramValue)) {String method = paramValue.toUpperCase(Locale.ENGLISH);if (ALLOWED_METHODS.contains(method)) {requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);}}}
​filterChain.doFilter((ServletRequest)requestToUse, response);}

HTTP Status 405 - JSPs only permit GET POST or HEAD问题的分析和解决办法相关推荐

  1. HTTP Status 405 - JSPs only permit GET POST or HEAD

    HTTP Status 405 - JSPs only permit GET POST or HEAD 版本:Spring4,Tomcat8 代码如下 web.xml: HiddenHttpMetho ...

  2. SpringMVC报错:HTTP Status 405 - JSPs only permit GET POST or HEAD

    最近在写JavaEE系列的文章,在写SpringMVC的REST风格URL的时候出现了一些问题,下面是部分代码. index.jsp页面代码: <%@ page language="j ...

  3. IIS 返回 405 - 不允许用于访问此页的 HTTP 谓词。终极解决办法!!!!

    首先这个问题在其他网站(CSDN,新浪博客等) 回答基本都是没有回答到"根本"上面来(而且总在纠结要不要勾选"全部谓词") 我是自己对比了本地IIS之后得出的结 ...

  4. RESTFUL风格中的405问题:JSPs only permit GET POST or HEAD

    RESTFUL风格中的405问题:JSPs only permit GET POST or HEAD 今天在学习RESTFUL风格的内容时,写完代码后遇到了如下图所示的405问题. 后台control ...

  5. HTTP Status 405 - HTTP method POST is not supported by this URL

    程序出现: HTTP Status 405 - HTTP method POST is not supported by this URL 发现原因如下 源程序: request.getRequest ...

  6. 配置过滤器报错 JSPs only permit GET POST or HEAD

    2019/4/10 问题描述 在web.xml中配置好过滤器,代码如下: <filter><filter-name>HiddenHttpMethodFilter</fil ...

  7. Maven Failed to deploy artifacts: status: 405 PUT

    问题现象 Failed to deploy artifacts: Could not transfer artifact  http-sign-request-client:jar:1.0.0 fro ...

  8. JSPs only permit GET POST or HEAD

    看了REST风格写测试程序,当PUT和DELETE时报405 出错时的代码 web.xml: <!-- 配置 org.springframework.web.filter.HiddenHttpM ...

  9. Nginx反向代理,返回405错误的解决办法

    https://www.linuxidc.com/Linux/2012-07/66760.htm Nginx的405错误解决办法 [日期:2012-07-30] 来源:Linux社区  作者:cnsa ...

最新文章

  1. Ueditor和CKeditor 两款编辑器的使用与配置
  2. python开发工资多少-Python开发工资多少
  3. idea 创建java文件_idea创建java文件 格式不对
  4. 什么是BNF EBNF 巴科斯范式及其扩展 BNF Augmented BNF
  5. 索引sql server_SQL Server索引–系列介绍
  6. lightGBM用于排序(Learning to Rank )
  7. SQL入门经典 第一章
  8. 最大同性恋交友网站 github 被微软收购,我不服!
  9. 微信小程序开发资源汇总
  10. idea类注释模板,方法注释模板。
  11. 获取时间差几小时几分钟前 (类似于新浪微博 发表于几小时几分钟前)
  12. 三重积分平均值_有关研究生考试中高等数学 分级中 “数农”是什么意思?
  13. 数商云采购管理系统支付结算功能详解,实现建筑工程企业采购业务智能化管理
  14. 【原创文章】PHP+MySQL 创建表单并将表单数据写入数据库中。
  15. C# 网络爬虫利器之Html Agility Pack如何快速实现解析Html
  16. 基于激光投影技术的虚拟键盘
  17. 大华摄像头抓拍图像实时下载
  18. 轻易解决VMware 虚拟机中被提示“请不要在虚拟机中运行此程序“
  19. Filter-url拦截
  20. 160429 vue.js 2 台灣小凡(体验 vuejs 2之随笔)

热门文章

  1. [ARM异常]-armv8/armv9异步异常类型、路由、屏蔽
  2. python实现DES算法
  3. ANSI,ASCII,UNICODE
  4. 16、IN和NOT IN用法详解
  5. 使用过滤器监控网站流量
  6. html实战例子: 简易的qq登录界面
  7. 自动化测试框架搭建-邮件-5
  8. 怎样从0开始搭建一个测试框架_0
  9. MySQL读写分离事务策略实现
  10. HTML中的列表和表格