restful设计风格及HTTP Status 405 - JSPs only permit GET POST or HEAD问题

      REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议;轻量级,跨平台,跨语言的架构设计;它是一种设计风格,不是一种标准,是一种思想。

      关于这个项目实例:

导入springmvc的框架jar等,保持版本号保持一致。(3.xxx对应的是jdk1.7,4.xxx对应的是jdk1.8)

     web.xml配置前置控制器和过滤器。过滤器是为了将普通浏览器的post请求转成delete和put。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 <display-name>springMVC001</display-name>
 <servlet>
	<servlet-name>springmvc</servlet-name>
 	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 	<init-param>
 		<param-name>contextConfigLocation</param-name>
 		<param-value>classpath:springmvc.xml</param-value>
 	</init-param>
 	<load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 	<servlet-name>springmvc</servlet-name>
 	<url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <!--为了解决一些浏览器不支持设置put和delete  -->
 <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>*.do</url-pattern>
 </filter-mapping>
 
 </web-app>

springmvc.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:p="http://www.springframework.org/schema/p"
    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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
       <!--设置扫描包  -->   
        <context:component-scan base-package="controller"/>
        <!--开启注解  -->
        <mvc:annotation-driven/>
        
</beans>

在这个hello1.jsp页面进行请求

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    四种表现方式
	<form action="rest/abdc.do" method="post">
	<input type="submit" value="增加"> 
	</form>
	<form action="rest/abdc.do" method="post">
	<input type="hidden" name="_method" value="DELETE"> 
	<input type="submit" value="删除"> 
	</form>
	<form action="rest/abdc.do" method="post">
	<input type="hidden" name="_method" value="PUT"> 
	<input type="submit" value="修改"> 
	</form>
	<form action="rest/abdc.do" method="get">
	<input type="submit" value="查看"> 
	</form>
	
</body>
</html>

请求处理类

@Controller
public class Restful {
@RequestMapping(value="/rest/{name}.do",method=RequestMethod.POST)
public String restinsert(@PathVariable("name") String name){
	System.out.println("增加"+name);
	return "/hello.jsp";
}

@RequestMapping(value="/rest/{name}.do",method=RequestMethod.DELETE)
public String restdelete(@PathVariable("name") String name){
	System.out.println("删除"+name);
	return "/hello.jsp";
	
}
@RequestMapping(value="/rest/{name}.do",method=RequestMethod.PUT)
public String restupdate(@PathVariable("name") String name){
	System.out.println("更新"+name);
	return "/hello.jsp";
	
}
@RequestMapping(value="/rest/{name}.do",method=RequestMethod.GET)
public String restselect(@PathVariable("name") String name){
	System.out.println("查看"+name);
	return "/hello.jsp";
	
}


}

处理请求后会跳转到hello.jsp中,但是会报错!!!

错误是这样的:JSPs only permit GET POST or HEAD

三种简单处理的办法!
1.tomcat换到7.0以及以下版本
2.请求先转给一个Controller,再返回jsp页面
3.在你的success页面头部文件将
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
    本人所使用的就是第三种就可以成功跳转了。

附上成功跳转页hello.jsp的代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"  isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	hello
	${name}
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_39404258/article/details/81980898