github還有這個功能,給力
甚至具體到了哪個依賴有問題
甚至甚至給出瞭解決方法
這個pom.xml是在csdn複製的,所以,我決定在去看看那篇博客,然後留個言
----------------------------------------------------------------------------------------------------------------------
REST風格。恩。。。我也不太懂,某乎上搜了下,有人喜歡有人討厭
百度百科是這麼說的
表述性狀態轉移是一組架構約束條件和原則。滿足這些約束條件和原則的應用程序或設計就是RESTful。
REST 定義了一組體系架構原則,您可以根據這些原則設計以系統資源爲中心的 Web 服務,
包括使用不同語言編寫的客戶端如何通過 HTTP 處理和傳輸資源狀態。
覺得想要描述自己的理解,但是覺得怎麼描述都不太準確,所以,擱置....
可能最開始我們的url是這樣的
增 丨 刪 丨 改 丨 查
insertxxx丨deletexxx 丨updatexxx 丨selectxxx
REST風格的url是這樣的
增 丨 刪 丨 改 丨 查
xxx 丨 xxx 丨 xxx 丨 xxx
恩,那我們根據什麼來區分請求的是哪個呢?
根據請求方式來區分要進行的操作是什麼,恩。。。
我覺得這些東西都能用來區分請求,但是大神選擇了post,get,put,delete,所以,理解了要做,不理解就先做着然後再理解。
恩。。具體看代碼吧。。。
首先web.xml需要配置一個filter攔截器
<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>
發起請求的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>
<!-- 發起圖書請求,使用rest風格的url地址
GET 查詢
DELETE 刪除
PUT 更新
POST 添加
-->
<a href="book/1"> 查詢</a>
<form action="book/1" method="post">
<input type="submit" value="添加"/>
</form>
<form action="book/1" method="post">
<input name="_method" value="delete">
<input type="submit" value="刪除"/>
</form>
<form action="book/1" method="post">
<input name="_method" value="put">
<input type="submit" value="更新"/>
</form>
</body>
</html>
因爲正常來講只能發起POST和GET請求,正巧了springMVC有對這個風格的支持所以如下:
delete 和put必須是post請求,並且參數中有_method參數
controller中的代碼如下
package com.sds.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;
@Controller
public class BookController {
@RequestMapping(value = "/book/{id}",method = RequestMethod.GET)
public String getBook(@PathVariable("id")String id) {
System.out.println("查詢到了"+id);
return "success";
}
@RequestMapping(value = "/book/{id}",method = RequestMethod.DELETE)
public String deleteBook(@PathVariable("id") String id) {
System.out.println("刪除了"+id);
return "success";
}
@RequestMapping(value = "/book/{id}",method = RequestMethod.PUT)
public String updateBook(@PathVariable("id") String id) {
System.out.println("更新了"+id);
return "success";
}
@RequestMapping(value = "/book/{id}",method = RequestMethod.POST)
public String postBook(@PathVariable("id") String id) {
System.out.println("添加了了"+id);
return "success";
}
}
然後跳轉success頁面如果出現如下,這個問題會出現在tomcat8.*的版本中,其他版本沒有測試,
HTTP Status 405 – 方法不允許啊
Type Status Report
消息 JSPs only permit GET POST or HEAD
描述 請求行中接收的方法由源服務器知道,但目標資源不支持
需要在頭部添加isErrorPage="true"結果如下
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>
spring-servlet.xml中我配置了這些:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.sds.controller"></context:component-scan>