當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
HTTP Status 405 - JSPs only permit GET POST or HEAD问题的分析和解决办法
生活随笔
收集整理的這篇文章主要介紹了
HTTP Status 405 - JSPs only permit GET POST or HEAD问题的分析和解决办法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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问题的分析和解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring之Bean的配置(二)
- 下一篇: Spring和MyBatis的整合