HTTP 405 的错误提示:消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS 的解决方法
如果项目是运行在 Tomcat 8 及以上,会发现发出的 PUT 请求和 DELETE 请求可以被控制其接收到,但是返回页面时(forward)会报HTTP 405 的错误提示:"消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS"。
解决方案:
使用 Tomcat 8 以下版本。
使用
@RestController或者@Controller + @ResponseBody标签,但是这样就无法跳转页面了。避免使用 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";
}
给 Tomcat 添加启动参数,使Tomcat允许写操作
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
创建一个新的 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>
在 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 的解决方法的更多相关文章
- sudo 提示 'xxx is not in the sudoers file.This incident will be reported.的解决方法'
在使用 Linux 的过程中,有时候需要临时获取 root 权限来执行命令时,一般通过在命令前添加 sudo 来解决. 但是第一次使用 sudo 时,有可能会得到这样一个错误提示 xxx is not ...
- Yii自定义错误提示消息
英文原文: http://www.yiiframework.com/wiki/1/how-to-customize-the-error-message-of-a-validation-rule/ Va ...
- SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法
SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法 SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图 ...
- ckeditor出现错误“从客户端(***)中检测到有潜在危险的 Request.Form值”的解决方法
ckeditor出现错误“从客户端(***)中检测到有潜在危险的 Request.Form值”的解决方法 页面中使用ckeditor,提交文章时总是出错,“从客户端(TextBox1="&l ...
- 安装TortoiseGit出现提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”-解决方法
我的系统是xp sp3安装TortoiseGit时出现了错误提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”. 解决方法,到微软官方下载相 ...
- 【win7下安装node.js错误:roling back action】与【"grunt" 不是内部或外部命令】 解决方法
[win7下安装node.js错误:roling back action] 解决方法: Node.js 服务器端的JavaScript Node.js 是一个基于Chrome JavaScript 运 ...
- jsp 页面导出excel时字符串数字变成科学计数法的解决方法
web导出excel数据格式化 原文地址:http://www.cnblogs.com/myaspnet/archive/2011/05/06/2038490.html 当我们把web页面上的数据 ...
- 阿里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 ...
- win7(32 bit) + IE8 环境,IE8无法弹窗(错误提示:“此网页上的错误可能会使它无法正确运行”),有关的系统注册信息损坏——解决方法
错误截图如下: IE有关的系统注册信息损坏,导致IE无法正常弹窗. 解决办法:重新注册与IE有关的DLL文件,具体如下: 1.以管理员身份运行附件脚本(新建txt文件,将下面代码复制到txt文 ...
随机推荐
- [转]【maven】解决Missing artifact jdk.tools:jdk.tools:jar:1.6
解决在pom.xml文件中出现的Missing artifact jdk.tools:jdk.tools:jar:1.6问题 <dependency> <groupId>jdk ...
- Contest 154
2019-09-16 17:22:28 总体感受:这次比赛的模版题也太多了吧,两条模版题没有想出来.总的来说,还是自己的刷题量还是严重的不够. 注意点: 1)提升刷题量和覆盖率非常重要: 2)在碰到大 ...
- [图中找环] Codeforces 659E New Reform
New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Data Management and Data Management Tools
Data Management ObjectivesBy the end o this module, you should understand the fundamentals of data m ...
- OpenCV-Python 轮廓分层 | 二十五
目标 这次我们学习轮廓的层次,即轮廓中的父子关系. 理论 在前几篇关于轮廓的文章中,我们已经讨论了与OpenCV提供的轮廓相关的几个函数.但是当我们使用cv.findcontour()函数在图像中找到 ...
- coding++:Spring Boot全局事务解释及使用(一)
Spring 事务的入口: TxAdviceBeanDefinitionParser 解释 <tx:advice/> 这里将解析tx的配置. @Override protected Cla ...
- coding++:解决Not allowed to load local resource错误-SpringBoot配置虚拟路径
1.在SpringBoot里上传图片后返回了绝对路径,发现本地读取的环节上面出现了错误(Not allowed to load local resource),一开始用的是直接本地路径. 但是在页面上 ...
- ehcahe + redis 实现多级缓存
1,了解数据存储的位置的不同 数据库:存储在磁盘上 redis:存储在内存上 ehcache:应用内缓存 缓存的目的:是为了将数据从一个较慢的介质上读取出来,放到一个较快的介质上,为了下次读取的时候更 ...
- ubuntu 如何在命令行打开当前目录
nautilus /var 打开var文件夹
- Premultiplied Alpha
Xcode 的工程选项里有一项 Compress PNG Files,会对 PNG 进行 Premultiplied Alpha.游戏开发中会更加关注这个格式,省一些运行时计算. Premultipl ...