如果项目是运行在 Tomcat 8 及以上,会发现发出的 PUT 请求和 DELETE 请求可以被控制其接收到,但是返回页面时(forward)会报HTTP 405 的错误提示:"消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS"

解决方案:

  1. 使用 Tomcat 8 以下版本。

  2. 使用 @RestController 或者 @Controller + @ResponseBody 标签,但是这样就无法跳转页面了。

  3. 避免使用 forward 方式跳转页面,改为 重定向redirect方式跳转到另一个控制器方法,再由这个控制器方法跳转页面。

    	@RequestMapping(value = "/rest", method = RequestMethod.PUT)
    public String put() {
    // 接收表单中的各种信息
    System.out.println("PUT --- 更新数据");
    return "redirect:/success";
    } @RequestMapping(value = "/success")
    public String success() {
    return "success";
    }
  4. 给 Tomcat 添加启动参数,使Tomcat允许写操作

    <init-param>
    <param-name>readonly</param-name>
    <param-value>false</param-value>
    </init-param>
  5. 创建一个新的 Filter 来过滤 FORWARD

    // HiddenHttpMethodFilter.java
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
    throws ServletException, IOException { HttpServletRequest requestToUse = request; if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
    String paramValue = request.getParameter(this.methodParam);
    if (StringUtils.hasLength(paramValue)) {
    requestToUse = new HttpMethodRequestWrapper(request, paramValue);
    }
    } filterChain.doFilter(requestToUse, response);
    }

    HiddenHttpMethodFilter 中的 doFilterInternal 方法是用来过滤 form 表单中 name 为 _method的请求。可以发现,它把请求作为参数传进 HttpMethodRequestWrapper 中并且返回了一个新的请求,放行的也是新的请求。所以我们可以重写 HttpMethodRequestWrapper 中的 getMethod() 方法,让它支持 forward 方式的跳转。

    // 重写 getMethod()
    package com.pudding.conf; import java.io.IOException; import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.HiddenHttpMethodFilter; public class MyHttpMethodFilter extends HiddenHttpMethodFilter { @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
    throws ServletException, IOException { HttpServletRequest requestToUse = request; String method = requestToUse.getMethod();
    if (method.equalsIgnoreCase("delete") || method.equalsIgnoreCase("put")) {
    method = "POST";
    } requestToUse = new HttpMethodRequestWrapper(request, method); filterChain.doFilter(requestToUse, response);
    } private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper { private final String method; public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
    super(request);
    this.method = method;
    } public String getMethod() {
    return this.method;
    }
    }
    }

    在 web.xml 中配置自己的过滤器:

    	<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>com.pudding.conf.MyHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
  6. 在 forward 需要跳转的页面头加上 isErrorPage="true"

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>success</h1>
    </body>
    </html>

HTTP 405 的错误提示:消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS 的解决方法的更多相关文章

  1. sudo 提示 'xxx is not in the sudoers file.This incident will be reported.的解决方法'

    在使用 Linux 的过程中,有时候需要临时获取 root 权限来执行命令时,一般通过在命令前添加 sudo 来解决. 但是第一次使用 sudo 时,有可能会得到这样一个错误提示 xxx is not ...

  2. Yii自定义错误提示消息

    英文原文: http://www.yiiframework.com/wiki/1/how-to-customize-the-error-message-of-a-validation-rule/ Va ...

  3. SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法

    SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法 SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图 ...

  4. ckeditor出现错误“从客户端(***)中检测到有潜在危险的 Request.Form值”的解决方法

    ckeditor出现错误“从客户端(***)中检测到有潜在危险的 Request.Form值”的解决方法 页面中使用ckeditor,提交文章时总是出错,“从客户端(TextBox1="&l ...

  5. 安装TortoiseGit出现提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”-解决方法

    我的系统是xp sp3安装TortoiseGit时出现了错误提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”. 解决方法,到微软官方下载相 ...

  6. 【win7下安装node.js错误:roling back action】与【"grunt" 不是内部或外部命令】 解决方法

    [win7下安装node.js错误:roling back action] 解决方法: Node.js 服务器端的JavaScript Node.js 是一个基于Chrome JavaScript 运 ...

  7. jsp 页面导出excel时字符串数字变成科学计数法的解决方法

    web导出excel数据格式化 原文地址:http://www.cnblogs.com/myaspnet/archive/2011/05/06/2038490.html   当我们把web页面上的数据 ...

  8. 阿里OSS Vue上传文件提示The OSS Access Key Id you provided does not exist in our records.解决方法

    vue项目 1.安装OSS的Node SDK npm install ali-oss --save 2.参考官方提示https://help.aliyun.com/document_detail/11 ...

  9. win7(32 bit) + IE8 环境,IE8无法弹窗(错误提示:“此网页上的错误可能会使它无法正确运行”),有关的系统注册信息损坏——解决方法

    错误截图如下:   IE有关的系统注册信息损坏,导致IE无法正常弹窗.   解决办法:重新注册与IE有关的DLL文件,具体如下: 1.以管理员身份运行附件脚本(新建txt文件,将下面代码复制到txt文 ...

随机推荐

  1. zookeeper基础学习-简介

    1.zookeeper的使命 zookeeper可以在分布式系统的协作多个任务(一个任务是指一个包含多个进程的任务),这个任务可以是为了协作或者是为了管理竞争. 协作:多个进程需要一同处理某些事情,一 ...

  2. PHP session反序列化

    先来了解一下关于session的一些基础知识 什么是session 在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 ...

  3. python txt文件批处理

    首先,切换文件路径到所在文件夹 然后,将txt文件内容按行读取,写入到all.txt def txtcombine(): files=glob.glob('*.txt') all = codecs.o ...

  4. Inception系列理解

    博客:博客园 | CSDN | blog 写在前面 Inception 家族成员:Inception-V1(GoogLeNet).BN-Inception.Inception-V2.Inception ...

  5. Python python 数据类型的相互转换

    # number 之间的相互转换 # int <=> float var1 = 1; print(type(var1)) #<class 'int'> res1 = float ...

  6. 运行redis数据库

    启动redis服务器:redis-server /usr/local/redis-5.0.5/etc/redis.conf 关闭: redis-cli shutdown启动客户端:进入bin文件夹,输 ...

  7. Linux的五种IO模型及同步和异步的区别

    前置知识 缓存 I/O 缓存 I/O 又被称作标准 I/O,大多数文件系统的默认 I/O 操作都是缓存 I/O.在 Linux 的缓存 I/O 机制中,操作系统会将 I/O 的数据缓存在文件系统的页缓 ...

  8. 在C#MVC三层项目中如何使用SprintNet

    0.添加dll文件 1.首先在根目录下新建一个文件夹[Config],然后新建2两个xml文件. 1-1[controllers.xml]用来配置需要创建的对象 1-2[service.xml]用来配 ...

  9. -bash: syntax error near unexpected token `newline'问题解决

    原因:bash语法错误,例如, 仔细查看发现语句中不能有'<'和'>',删除这两个符号即可: 问题解决!

  10. Python实现压缩解压缩,移动复制copy文件

    import shutil import os from shutil import make_archive # 查看可压缩的文件类型 print(shutil.get_archive_format ...