SpringMVC框架——集成RESTful架构
REST:Representational State Transfer 资源表现层状态转换
Resources 资源
Representation 资源表现层
State Transfer 状态转换
RESTful的特点:
- URL传参更加简洁
传统的URL:http://localhost:7777/test?id=1
RESTful:http://localhost:7777/test/1
- 完成不同终端之间的资源共享,RESTful提供了一套规范,不同终端之间只需要遵守该规范,就可以实现数据的交互。
RESTful具体来讲就是4种表现形式,HTTP协议中的四种请求类型(GET、POST、PUT、DELETE)分别对应四种常规操作,CRUD。
GET 用来获取资源
POST 用来创建资源
PUT 用来修改资源
DELETE用来删除资源
- 由于form表单不支持PUT、DELETE请求,需要想办法支持。SpringMVC通过添加过滤器的方式来完成这种处理的。
实现原理:
检测请求参数中是否包含 _method 参数,如果包含则获取该参数的值,判断是哪种操作后完成请求类型的转换,然后继续传递。
示例一:
1、在 form 表单中添加隐藏域标签, name="_method", value="PUT"/"DELETE"。
2、在web.xml文件中配置HiddenHttpMethodFilter
<!-- put/delete请求转换 -->
<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>
3、编写Controller
package com.sunjian.controller;
import com.sunjian.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
/**
* @author sunjian
* @date 2020/3/19 8:13
*/
@Controller
@RequestMapping("/rest")
public class RESTController {
@RequestMapping("/get/{id}")
@ResponseBody
public String get(@PathVariable("id") int num){
System.out.println("GET");
if(num < 100){
return "OK";
}else
return "没有这个编号的数据";
}
@RequestMapping(value = "/post", method = RequestMethod.POST)
public ModelAndView post(){
User user = new User();
user.setId(123);
user.setName("post");
user.setAge(55);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user", user);
modelAndView.setViewName("show");
return modelAndView;
}
@RequestMapping(value = "/put", method = RequestMethod.PUT)
@ResponseBody // 返回json格式
public String put(){
System.out.println("put");
return "put";
}
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
@ResponseBody
public String delete(){
System.out.println("delete");
return "delete";
}
}
4、编写JSP页面
rest.jsp
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %> <!-- 是否忽略解析el表达式 -->
<html>
<body>
<form action="/rest/put" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="PUT">
</form>
<hr>
<form action="/rest/delete" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="DELETE">
</form>
<hr>
<form action="/rest/get/1" method="GET">
<input type="submit" value="GET">
</form>
<hr>
<form action="/rest/post" method="POST">
<input type="submit" value="POST">
</form>
</body>
</html>
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${user}
</body>
</html>
5、访问jsp页面,提交请求





示例二:
实现课程表的增删改查。
1、实体类
package com.sunjian.entity;
/**
* @author sunjian
* @date 2020/3/20 14:28
*/
public class Course {
private Integer id;
private String name;
private Double price;
public Course(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Course{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
2、编写repository
package com.sunjian.repository;
import com.sunjian.entity.Course;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @author sunjian
* @date 2020/3/20 14:29
*/
@Repository
public class CourseRepository {
private static Map<Integer, Course> map;
static {
map = new HashMap<>();
map.put(1, new Course(1, "java", 2000d));
map.put(2, new Course(2, "python", 3000d));
map.put(3, new Course(3, "c++", 4000d));
}
public void saveOrUpdate(Course course){
map.put(course.getId(), course);
}
public Collection<Course> findAll(){
return map.values();
}
public Course findId(Integer id){
return map.get(id);
}
public void deleteById(Integer id){
map.remove(id);
}
}
3、编写controller
package com.sunjian.controller;
import com.sunjian.entity.Course;
import com.sunjian.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
/**
* @author sunjian
* @date 2020/3/20 14:34
*/
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseRepository courseRepository;
@PostMapping("/add")
public String add(Course course){
courseRepository.saveOrUpdate(course);
return "redirect:/course/findAll";
}
@DeleteMapping("/deleteById/{id}")
public String deleteById(@PathVariable("id") Integer id){
courseRepository.deleteById(id);
return "redirect:/course/findAll";
}
@PutMapping("/update")
public String update(Course course){
courseRepository.saveOrUpdate(course);
return "redirect:/course/findAll";
}
@GetMapping("/findAll")
public ModelAndView findAll(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("showCourse");
modelAndView.addObject("list", courseRepository.findAll());
return modelAndView;
}
@GetMapping("/findById/{id}")
public ModelAndView findById(@PathVariable("id") Integer id){
ModelAndView modelAndView = new ModelAndView("updateCourse");
modelAndView.addObject("course", courseRepository.findId(id));
return modelAndView;
}
}
4、配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--中文乱码处理-->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- put/delete请求转换 -->
<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>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- js -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<!-- jpg -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
</web-app>
5、配置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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 配置自动扫描 -->
<context:component-scan base-package="com.sunjian"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 消息转换器 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 自定义数据类型转换器 -->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.sunjian.converter.DataConverter">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
</bean>
<bean class="com.sunjian.converter.goodsConverter"></bean>
</list>
</property>
</bean>
<!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 处理文件名中文乱码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 多文件上传总⼤小的上限 10M -->
<property name="maxUploadSize" value="10485760"></property>
<!-- 单文件大小的上限 1M -->
<property name="maxUploadSizePerFile" value="1048576"></property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>
6、编写jsp页面
showCourse.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>课程ID</th>
<th>课程名称</th>
<th>课程价格</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="course">
<tr>
<td>${course.id}</td>
<td>${course.name}</td>
<td>${course.price}</td>
<td>
<form action="/course/deleteById/${course.id}" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="删除"/>
</form>
<a href="/course/findById/${course.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<a href="/addCourse.jsp">添加课程</a>
</body>
</html>
addCourse.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/course/add" method="post">
课程编号:<input type="text" name="id"/><br/>
课程名称:<input type="text" name="name"/><br/>
课程价格:<input type="text" name="price"/><br/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
updateCourse.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/course/update" method="post">
课程编号:<input type="text" value="${course.id}" name="id" readonly/><br/>
课程名称:<input type="text" value="${course.name}" name="name"/><br/>
课程价格:<input type="text" value="${course.price}" name="price"/><br/>
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="修改"/>
</form>
</body>
</html>
7、访问页面操作



OK.
SpringMVC框架——集成RESTful架构的更多相关文章
- 8.springMVC中的RESTful架构风格
RESTful架构:是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 关于 ...
- springMVC 中的restful 架构风格
RESTful架构 : 是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. ...
- springMVC框架集成tiles模板
将tiles模板集成到springMVC框架下,大概流程如下: 1.在配置文件中加入tiles支持 我的servlet配置文件名为spring-mvc.xml.具体配置如下: <?xml ver ...
- SpringMVC框架04——RESTful入门
1.RESTful的基本概念 REST(Representational State Transfer)表述性状态转移,REST并不是一种创新技术,它指的是一组架构约束条件和原则,符合REST的约束条 ...
- springMvc框架之Restful风格
method: @Controller @RequestMapping("/test") public String MyController{ @RequestMapping(& ...
- RabittMQ实践(二): RabbitMQ 与spring、springmvc框架集成
一.RabbitMQ简介 1.1.rabbitMQ的优点(适用范围)1. 基于erlang语言开发具有高可用高并发的优点,适合集群服务器.2. 健壮.稳定.易用.跨平台.支持多种语言.文档齐全.3. ...
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)
环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试
环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试【转】
环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...
随机推荐
- 吴裕雄--天生自然python编程:实例(3)
# 返回 x 在 arr 中的索引,如果不存在返回 -1 def binarySearch (arr, l, r, x): # 基本判断 if r >= l: mid = int(l + (r ...
- 吴裕雄--天生自然 人工智能机器学习实战代码:LASSO回归
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model from s ...
- python pip配置以及安装工具包的一些方法
pip是python的一个工具包管理工具,可以下载安装需要的工具包,想要使用它来管理工具包首先要安装pip,安装方法可以参照下面这个网址来进行: https://www.cnblogs.com/Nan ...
- Problem 43 // Project Euler
Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up ...
- Ansible(一) Try it - 枯鱼的博客
学习ansible的最好方式就是使用,先别管什么inventory,playbook,module这些.按照安装文档安装,然后try it,一边学一边体验,这样的速度是最快的.当熟悉了之后,想要深入就 ...
- ES6学习笔记(三):教你用js面向对象思维来实现 tab栏增删改查功能
前两篇文章主要介绍了类和对象.类的继承,如果想了解更多理论请查阅<ES6学习笔记(一):轻松搞懂面向对象编程.类和对象>.<ES6学习笔记(二):教你玩转类的继承和类的对象>, ...
- ElasticSearch系列四 CURD
1: ES 类似JPA操作 1.1 编写实体类 1.2 编写映射文件 xxx.json 1.3编写repository继承 ElasticSearchrepository 1.4 编写admin 的C ...
- 【Eclipse】eclipse设置,为了更简单快捷的开发
保存时自动导包 Windows->Perferences->Java->Editor->Save Actions
- 基本类型和引用类型的值 [重温JavaScript基础(一)]
前言: JavaScript 的变量与其他语言的变量有很大区别.JavaScript 变量松散类型的本质,决定了它只是在特定时间用于保存特定值的一个名字而已.由于不存在定义某个变量必须要保存何种数据类 ...
- python类变量与构造函数的使用
类变量:可在类的所有实例之间共享的变量 实例类对象:类的实例是调用类对象来创建的.如:par = Parent(),par就是类Parent的一个实例类对象. 实例变量(成员变量):同一个类对象可以创 ...