Rest风格的URL地址约束||高版本Tomcat;Rest支持有点问题
生活随笔
收集整理的這篇文章主要介紹了
Rest风格的URL地址约束||高版本Tomcat;Rest支持有点问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Rest:系統(tǒng)希望以非常簡潔的URL地址來發(fā)請求;
? ? ? ? ? 怎樣表示對一個資源的增刪改查用請求方式來區(qū)分
/getBook?id=1 ? :查詢圖書
/deleteBook?id=1:刪除1號圖書
/updateBook?id=1:更新1號圖書
/addBook ? ? :添加圖書
Rest推薦;
? ? ?url地址這么起名; ?/資源名/資源標(biāo)識符
/book/1 ? ? ? ? ?:GET-----查詢1號圖書
/book/1 ? ? ? ? ?:PUT------更新1號圖書
/book/1 ? ? ? ? ?:DELETE-----刪除1號圖書
/book ? ? ? ? ? ? ? :POST-----添加圖書
系統(tǒng)的URL地址就這么來設(shè)計即可;
? ? ?簡潔的URL提交請求,以請求方式區(qū)分對資源操作;
問題:從頁面上只能發(fā)起兩種請求,GET、POST;
其他的請求方式?jīng)]法使用;
發(fā)起圖書的增刪改查請求;使用Rest風(fēng)格的URL地址;
注意:高版本Tomcat;Rest支持有點問題?
解決方法
index.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><a href="book/1">查詢圖書</a><br/><br/><form action="book" method="post"><input type="submit" value="添加1號圖書"/> </form><br/><!-- 發(fā)送DELETE請求 --> <form action="book/1" method="post"><input name="_method" value="delete"/><input type="submit" value="刪除1號圖書"/> </form><br/><!-- 發(fā)送PUT請求 --> <form action="book/1" method="post"><input name="_method" value="put"/><input type="submit" value="更新1號圖書"/> </form><br/></body> </html>web.xml
<?xml version="1.0" encoding="UTF-8"?> <!--suppress ALL --> <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>3.SpringMVC_rest</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><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></web-app>springmvc-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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:component-scan base-package="com.atguigu"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean></beans>BookController.java
package com.atguigu.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 {/*** 處理查詢圖書請求* @param id* @return*/@RequestMapping(value="/book/{bid}",method=RequestMethod.GET)public String getBook(@PathVariable("bid")Integer id) {System.out.println("查詢到了"+id+"號圖書");return "success";}/*** 圖書刪除* @param id* @return*/@RequestMapping(value="/book/{bid}",method=RequestMethod.DELETE)public String deleteBook(@PathVariable("bid")Integer id) {System.out.println("刪除了"+id+"號圖書");return "success";}/*** 圖書更新* @return*/@RequestMapping(value="/book/{bid}",method=RequestMethod.PUT)public String updateBook(@PathVariable("bid")Integer id) {System.out.println("更新了"+id+"號圖書");return "success";}@RequestMapping(value="/book",method=RequestMethod.POST)public String addBook() {System.out.println("添加了新的圖書");return "success";} }success.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><h1>你成功了,6666</h1> </body> </html>總結(jié)
以上是生活随笔為你收集整理的Rest风格的URL地址约束||高版本Tomcat;Rest支持有点问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC入门案例细节分析
- 下一篇: @Value获取值和@Configura