Restful
- 1、入門
- 1.1 簡介
- 1.2 實例
1、入門
1.1 簡介
RESTFul是什么
RESTFul是WEB服務接口的一種設計風格。
RESTFul定義了一組約束條件和規范,可以讓WEB服務接口更加簡潔、易于理解、易于擴展、安全可靠。
1.2 實例
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_4_0.xsd"version="4.0"><!--字符編碼過濾器--><filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--隱藏的HTTP請求方式過濾器--><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><servlet><servlet-name>Springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><!-- 初始化參數時 可以指定springmvc配置文件的位置 以及名稱--><!-- 其默認是存放在WEB-INF 下 名稱默認是<servlet-name>-servlet.xml--><param-name>contextConfigLocation</param-name><!-- 表示在根路徑下 即放在resources目錄下 名稱是 springmvc.xml--><param-value>classpath:springmvc.xml</param-value></init-param><!-- 這是一個優化策略 表示在服務器啟動時就初始化DispatcherServlet 而不是在用戶第一次請求時 可以增加用戶體驗--><load-on-startup>0</load-on-startup></servlet><servlet-mapping><servlet-name>Springmvc</servlet-name><url-pattern>/</url-pattern></servlet-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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.cky"></context:component-scan><!--視圖解析器--><bean id="thymeleafViewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver"><!--作用于視圖渲染的過程中,可以設置視圖渲染后輸出時采用的編碼字符集--><property name="characterEncoding" value="UTF-8"/><!--如果配置多個視圖解析器,它來決定優先使用哪個視圖解析器,它的值越小優先級越高--><property name="order" value="1"/><!--當 ThymeleafViewResolver 渲染模板時,會使用該模板引擎來解析、編譯和渲染模板--><property name="templateEngine"><bean class="org.thymeleaf.spring6.SpringTemplateEngine"><!--用于指定 Thymeleaf 模板引擎使用的模板解析器。模板解析器負責根據模板位置、模板資源名稱、文件編碼等信息,加載模板并對其進行解析--><property name="templateResolver"><bean class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver"><!--設置模板文件的位置(前綴)--><property name="prefix" value="/WEB-INF/templates/"/><!--設置模板文件后綴(后綴),Thymeleaf文件擴展名不一定是html,也可以是其他,例如txt,大部分都是html--><property name="suffix" value=".html"/><!--設置模板類型,例如:HTML,TEXT,JAVASCRIPT,CSS等--><property name="templateMode" value="HTML"/><!--用于模板文件在讀取和解析過程中采用的編碼字符集--><property name="characterEncoding" value="UTF-8"/></bean></property></bean></property></bean><!--配置視圖控制器--><mvc:view-controller path="/c" view-name="c"/><!--開啟注解驅動--><mvc:annotation-driven></mvc:annotation-driven><!--靜態資源訪問配置--><mvc:resources mapping="/static/**" location="/static/"/>
<!-- 或者 <mvc:default-servlet-handler></mvc:default-servlet-handler>-->
</beans>
項目目錄
Mycontroller.java
package com.cky.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 Mycontroller {
@RequestMapping(value = "/users",method = RequestMethod.GET)
public String a(){System.out.println("查詢所有");return "ok";
}
@RequestMapping("/")
public String index(){return "index";
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
public String getone(@PathVariable("id") int id){System.out.println("查詢1號");return "ok";
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.POST)
public String addusers(@PathVariable("id") int id){System.out.println("新增用戶");return "ok";
}@RequestMapping(value = "/users/{id}",method = RequestMethod.PUT)
public String modify(@PathVariable("id") int id){System.out.println("修改用戶");return "ok";
}@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)public String delete(@PathVariable("id") int id){System.out.println("刪除用戶");return "ok";}
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>restful</title>
</head>
<body>
<!--默認get-->
<a th:href="@{/users}">查詢所有</a>
<a th:href="@{/users/1}">查詢1號</a>
<!--新增-->
<form th:action="@{/users/100}" method="post"><input type="text" placeholder="用戶名"><input type="submit" value="提交">
</form>
<!--修改-->
<form th:action="@{/users/100}" method="post"><input type="hidden" name="_method" value="put"><input type="text" placeholder="用戶名"><input type="submit" value="修改">
</form><a th:href="@{/users/1}" onclick="del(event)">刪除1號用戶</a>
<form id="delForm" method="post"><input type="hidden" name="_method" value="delete">
</form>
<script>function del(event){//獲取表單let delForm=document.getElementById("delForm");//給表單賦值delForm.action=event.target.href;//發送post請求delForm.submit();//阻止超鏈接默認行為event.preventDefault();}
</script>
</body>
</html>