25、(知識篇)SpringMVC02 REST風格url測試

/**

* SpringMVC REST風格url測試

* 1、首先需要在web.xml中配置一個filter:org.springframework.web.filter.HiddenHttpMethodFilter

* 2、在請求form表單中使用隱藏域:hidden設置 _method 得值爲 DELETE/PUT  (這個可以在spring源碼追溯到)

* 3、額外知識:使用tomcat8 請求delete和put時會報如下錯誤:

* HTTP Status 405 - JSPs only permit GET POST or HEAD.。

* 用tomcat7則沒有錯

* @return

*/

測試類:

package com.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

	/**
	 * SpringMVC REST風格url測試
	 * 1、首先需要在web.xml中配置一個filter:org.springframework.web.filter.HiddenHttpMethodFilter
	 * 2、在請求form表單中使用隱藏域:hidden設置 _method 得值爲 DELETE/PUT  (這個可以在spring源碼追溯到)
	 * 3、額外知識:使用tomcat8 請求delete和put時會報如下錯誤:
	 * HTTP Status 405 - JSPs only permit GET POST or HEAD.。
	 * 用tomcat7則沒有錯
	 * 
	 * @return
	 */
	
	@RequestMapping("/")
	public String index(){
		return "index";
	}
	
	@RequestMapping(value="testRest/{id}",method=RequestMethod.DELETE)
	@ResponseBody()
	public String testRestDelete(@PathVariable(value="id") int id){
		System.out.println("delete id == "+id);
		return "success";
	}
	
	@RequestMapping(value="testRest/{id}",method=RequestMethod.PUT)
	public String testRestPut(@PathVariable(value="id") int id){
		System.out.println("put id == "+id);
		return "success";
	}
	
	@RequestMapping(value="testRest",method=RequestMethod.POST)
	public String testRestPost(){
		System.out.println("POST");
		return "success";
	}
	
	@RequestMapping(value="testRest/{id}",method=RequestMethod.GET)
	public String testRest(@PathVariable(value="id") int id){
		System.out.println("get id == "+id);
		return "success";
	}
	
}

spring-mvc.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<context:component-scan base-package="com.spring"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

web.xml

<?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"
	id="WebApp_ID" version="3.0">
	<display-name>25SpringMVC02</display-name>
	<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>

	<!-- The front controller of this Spring Web application, responsible for 
		handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

web-inf/views/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="testRest/1" method="get">
		<input type="submit" value="GET請求">
	</form>
	<br>
	<form action="testRest" method="post">
		<input type="submit" value="POST請求">
	</form>
	<br>
	<form action="testRest/2" method="post">
		<input type="hidden" value="put" name="_method">
		<input type="submit" value="PUT請求">
	</form>
	<br>
	<form action="testRest/3" method="post">
		<input type="hidden" value="delete" name="_method">
		<input type="submit" value="DELETE請求">
	</form>
	<br>
</body>
</html>

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	Hello Spring!
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章