Restful style:
A software architecture style, design style, not a standard, only provides a set of design principles and constraints. It is mainly used for client and server interaction software. The software designed based on this style can be more concise, more hierarchical, and easier to implement caching and other mechanisms.
URL definition
Resources: all things on the Internet can be abstracted as resources
Resource operation: use POST, DELETE, PUT and GET to operate resources using different methods.
Add, delete, modify and query respectively.
Restful features
Resources: an entity on the network, or a specific information on the network. It can be a text, a picture, a song, a service, in short, it is a concrete existence. A URI (uniform resource locator) can be used to point to it, and each resource corresponds to the URI of a feature. To obtain this resource, you can access its URI, so the URI is the unique identifier of each resource.
Presentation layer: the form in which resources are concretely presented, which is called its presentation layer. For example, text can be expressed in txt format, HTML format, XML format, JSON format, or even binary format.
State transfer: each request represents an interactive process between the client and the server. HTTP protocol is a stateless protocol, that is, all States are saved on the server side. Therefore, if the client wants to operate the server, it must use some means to make the server "state transfer". This transformation is based on the presentation layer, so it is "presentation layer state transformation". Specifically, in the HTTP protocol, there are four verbs representing the operation mode: GET, POST, PUT and DELETE. They correspond to four basic operations: GET is used to obtain resources, POST is used to create new resources, PUT is used to update resources, and DELETE is used to DELETE resources.
Next, simply implement:
First: create an entity class:
package gvn.openlab.bean; public class Employee { private int id;//id simulation database self increment private String name; private String sex; private static int i=0; public Employee() { this.id = i; i++; } public Employee( String name, String sex) { this(); this.name = name; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
When establishing an interface, define the corresponding method:
package gvn.openlab.dao; import gvn.openlab.bean.Employee; import java.util.List; public interface EmpDao { public List<Employee> findall();//Find all data public void sava (Employee employee);//Add data method public void delete(Integer id);//Delete data by id public Employee findByid(Integer id);//Find data by id }
Interface implementation method:
package gvn.openlab.dao.impl; import gvn.openlab.bean.Employee; import gvn.openlab.dao.EmpDao; import org.springframework.stereotype.Controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller public class EmpDaoImpl implements EmpDao { private static Map<Integer,Employee> map=new HashMap<Integer,Employee>();//Define a map fake data static {//Static code block loading data into map Employee e=new Employee("Ruochen","male"); Employee e1=new Employee("Ruoxi","male"); Employee e2=new Employee("Ziyu","male"); map.put(e.getId(),e); map.put(e1.getId(),e1); map.put(e2.getId(),e2); System.out.println("sta Method execution"); } @Override public List<Employee> findall() {//Search data // System.out.println(new ArrayList<>(map.values())); return new ArrayList<>(map.values());//Encapsulate map into List } @Override public void sava(Employee employee) {//Add method map.put( employee.getId(),employee); } @Override public void delete(Integer id) {//Delete method map.remove(id); } @Override public Employee findByid(Integer id) {//Find data by id return map.get(id); } }
There is no connection to the database, only false data;
jsp file:
First enter the interface:
<%-- Created by IntelliJ IDEA. User: 11960 Date: 2021/9/21 Time: 16:31 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <a href="${pageContext.request.contextPath}/emp">see information</a> </body> </html>
The interface does not need a background, so it can be configured in 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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="gvn.openlab"> <!-- scanning gvn.openlab--> </context:component-scan> <!-- We configured<mvc:view-controller path="/" view-name="index"></mvc:view-controller>If you do not write the following configuration, others that need background control cannot be used--> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--If the current path is/ Give it to the corresponding view parser to directly parse it into a view--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <mvc:view-controller path="/save" view-name="save"></mvc:view-controller> </beans>
jsp file showing the query interface:
//When we use < C: foreach var = "" items = "" > add the following, otherwise we can't use it <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <a href="${pageContext.request.contextPath}/save">add to</a> //Click Add to jump to the sava interface <table> <tr> <td>id</td> <td>name</td> <td>sex</td> </tr> //Loop to add the data of the list to the following <c:forEach var="employee" items="${list}"> <tr> <td> ${employee.id} </td> <td> ${employee.name} </td> <td>${employee.sex}</td> <td> //Clicking modify will disable the empupdate method of the EmployeeController class <a href="${pageContext.request.contextPath}/emp/${employee.id}">modify</a> </td> <td> //Clicking delete will disable the empdelete method of the EmployeeController class <a href="javascript:void(0)" onclick="deletedate(${employee.id});">delete</a> </td> </tr> </c:forEach> </table>//Change the request mode to DELETE <form action="" method="post" name="from"> <input type="hidden" name="_method" value="DELETE" /> // name="_method" copy this attribute as much as possible, otherwise errors may occur </form> </body> <script> //Clicking delete will call the following function function deletedate(id){ var form=document.getElementsByName("from")[0]; form.action="${pageContext.request.contextPath}/emp/"+id; form.submit(); } </script> </html>
EmployeeController control class
package gvn.openlab.controller; import gvn.openlab.bean.Employee; import gvn.openlab.dao.EmpDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; @Controller public class EmployeeController { @Autowired @Qualifier("empDaoImpl") private EmpDao empDaoimpl; //View all employee information @GetMapping("/emp") public String empilist(HttpServletRequest request){ List<Employee> list=empDaoimpl.findall(); System.out.println(list); request.setAttribute("list",list); return "show";//forward //Return to < property name = "prefix" value = "/ Web inf / views / > < / property > under xml // <property name="suffix" value=".jsp"></property> //Go to find the show.jsp file under WEB-INF/views } //When the request method is post and the url is "/ emp", call the following method @PostMapping("/emp") public String empsava(Employee employee){ empDaoimpl.sava(employee); return "redirect:/emp";//redirect } @DeleteMapping("/emp/{id}") public String empdelete(@PathVariable("id") Integer id){ empDaoimpl.delete(id); return "redirect:/emp"; } @GetMapping ("/emp/{id}") public String empupdate(@PathVariable("id") Integer id,Map<String,Object>map){ Employee employee=empDaoimpl.findByid(id); map.put("employee",employee);//Find what you want to modify first return "empupdate"; } @PutMapping("/emp") public String empupdate(Employee employee ){ empDaoimpl.sava(employee); return "redirect:/emp"; } }
Added jap files:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> //Click Add to skip the empilist method <form action="${pageContext.request.contextPath}/emp" method="post"> name:<input type="text" name="name"><br> sex:<input type="radio" name="sex" value="male">male <input type="radio" name="sex" value="female">female <br> <input type="submit" value="add to"> </form> </body> </html>
Updated jsp file:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/emp" method="post"> id:<input type="text" name="id" value="${employee.id}"><br> name:<input type="text" name="name" value="${employee.name}"><br> sex:<input type="radio" name="sex" value="male" ${employee.sex=="male"?"checked":""}>male <input type="radio" name="sex" value="female" ${employee.sex=="female"?"checked":""}>female <br> <input type="hidden" name="_method" value="put" /> <input type="submit" value="modify"> </form> </body> </html>
result:
pom.xml dependency:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>mvc-spring</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.5</version> </dependency> <!--Spring Thing dependence --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.4.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/taglibs/standard --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>