當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
HTTP Status 405 - JSPs only permit GET POST or HEAD问题的分析和解决办法
生活随笔
收集整理的這篇文章主要介紹了
HTTP Status 405 - JSPs only permit GET POST or HEAD问题的分析和解决办法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.出錯(cuò)時(shí)的代碼
(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請(qǐng)求轉(zhuǎn)為DELETE請(qǐng)求和PUT請(qǐng)求 --><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 --><!-- 默認(rèn)的配置文件為 /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"> ?<!--自動(dòng)掃描包中的Controlller --><context:component-scan base-package="com.itheima.controller"/><!-- 配置視圖解析器: 如何把 handler 方法返回值解析為實(shí)際的物理視圖 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><!-- 前綴 --><property name="suffix" value=".jsp"/><!-- 后綴,自動(dòng)拼接 --></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.錯(cuò)誤原因分析
當(dāng)在index.jsp中提交后,HiddenHttpMethodFilter會(huì)將method轉(zhuǎn)換成對(duì)應(yīng)的DELETE或PUT,SpingMVC會(huì)繼續(xù)用對(duì)應(yīng)的請(qǐng)求重定向到success.jsp中,而jsp只支持GET、POST、HEAD請(qǐng)求方法。
3.解決方法
(1)為controller里的方法加上@ResponseBody()注解,并且返回一個(gè)字符串。不過(guò)此方法無(wú)法進(jìn)入指定success.jsp頁(yè)面。
?@RequestMapping(value = "order", method = RequestMethod.PUT)@ResponseBody()public String testOrderPut(@RequestParam("id") Integer id) {System.out.println("更新id為" + id + "的員工");return "success";}(2)使用重定向跳轉(zhuǎn)到指定頁(yè)面
?@RequestMapping(value = "order", method = RequestMethod.DELETE)public String testOrderDelete(@RequestParam("id") Integer id) {System.out.println("刪除id為" + id + "的員工");//無(wú)法重定向到WEB-INF目錄中,若要訪問(wèn),需把該頁(yè)面放入其它路徑return "redirect:success.jsp";}(3)taocat換到7.0以及以下版本
(4)在你的success頁(yè)面頭部文件設(shè)置其頁(yè)面為錯(cuò)誤頁(yè)面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>4.HiddenHttpMethodFilter轉(zhuǎn)換請(qǐng)求的源碼
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);}?
總結(jié)
以上是生活随笔為你收集整理的HTTP Status 405 - JSPs only permit GET POST or HEAD问题的分析和解决办法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring之Bean的配置(二)
- 下一篇: Spring和MyBatis的整合