SpringMVC框架04——RESTful入门
1、RESTful的基本概念
REST(Representational State Transfer)表述性状态转移,REST并不是一种创新技术,它指的是一组架构约束条件和原则,符合REST的约束条件和原则的架构,就称它为RESTful架构。
RESTful具体来讲就是HTTP协议的四种形式表示四种基本操作:
GET(获取资源)、POST(新建资源)、PUT(修改资源)、DELETE(删除资源)
2、RESTful架构的特点
统一了客户端访问资源的接口
url更加简洁,易于理解,便于扩展
有利于不同系统之间的资源共享
3、RESTful开发风格示例
- 查询课程,method='get',http://localhost:8080/id
- 添加课程,method='post',http://localhost:8080/course
- 删除课程,method='delete',http://localhost:8080/id
- 修改课程,method='put',http://localhost:8080/course
4、RESTful的代码实现
4.1、配置过滤器
在web.xml配置文件中编辑代码:
<!--设置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>
4.2、创建实体类
示例代码:
public class Course {
private int id;
private String name;
private double price;
//getter,setter方法
}
4.3、创建DAO
示例代码:
@Repository
public class CourseDao { //模拟数据库存储
private Map<Integer, Course> courseMap = new HashMap<>(); /**
* 添加课程
*/
public void add(Course course){
courseMap.put(course.getId(),course);
} /**
* 查询所有课程
*/
public Collection<Course> getAll(){
return courseMap.values();
} /**
* 通过ID查询课程
*/
public Course getById(int id){
return courseMap.get(id);
} /**
* 修改课程
*/
public void update(Course course){
courseMap.put(course.getId(),course);
} /**
* 删除课程
*/
public void delete(int id){
courseMap.remove(id);
} }
4.4、POST添加数据
Controller类中的业务方法:
@Controller
public class CourseController { @Autowired
private CourseDao courseDao; /**
* 添加课程
*/
@PostMapping("/add")
public String add(Course course){
courseDao.add(course);
System.out.println(course);
return "redirect:/getAll";
}
}
add.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>添加课程</title>
</head>
<body>
<h1>添加课程</h1>
<hr>
<form action="${pageContext.request.contextPath}/add" method="post">
<p>
课程编号:<input type="text" name="id">
</p>
<p>
课程名称:<input type="text" name="name">
</p>
<p>
课程价格:<input type="text" name="price">
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
</body>
</html>
4.5、GET查询数据
Controller类中的业务方法:
/**
* 查询所有课程
*/
@GetMapping("/getAll")
public String getAll(Model model){
model.addAttribute("courses",courseDao.getAll());
System.out.println("getAll.......");
return "index";
} /**
* 根据ID查询课程
*/
@GetMapping("/getById/{id}")
public String getById(@PathVariable("id") int id,Model model){
model.addAttribute("course",courseDao.getById(id));
return "edit";
}
index.jsp页面,用于展示数据:
<table border="1" width="80%">
<tr>
<th>课程编号</th>
<th>课程名称</th>
<th>课程价格</th>
<th>编辑</th>
</tr>
<c:forEach items="${courses}" var="course">
<tr>
<td>${course.id}</td>
<td>${course.name}</td>
<td>${course.price}</td>
<td>
<form action="${pageContext.request.contextPath}/getById/${course.id}" method="get">
<input type="submit" value="编辑">
</form>
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="删除">
</form>
</td>
</tr>
</c:forEach>
</table>
4.6、PUT修改数据
Controller类中的业务方法:
/**
* 修改课程
*/
@PutMapping("/update")
public String update(Course course){
courseDao.update(course);
return "redirect:/getAll";
}
edit.jsp页面
<form action="${pageContext.request.contextPath}/update" method="post">
<p>
课程编号:<input type="text" name="id" value="${course.id}">
</p>
<p>
课程名称:<input type="text" name="name" value="${course.name}">
</p>
<p>
课程价格:<input type="text" name="price" value="${course.price}">
</p>
<p>
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="提交">
</p>
</form>
4.7、DELETE删除数据
Controller类中的业务方法:
/**
* 删除课程
*/
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable("id") int id){
courseDao.delete(id);
return "redirect:/getAll";
}
index.jsp页面中的删除按钮:
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="删除">
</form>
SpringMVC框架04——RESTful入门的更多相关文章
- [jbdj]SpringMVC框架(1)快速入门
1)springmvc快速入门(传统版) 步一:创建springmvc_demo一个web应用 步二:导入springioc,springweb , springmvc相关的jar包 步三:在/WEB ...
- springMvc框架之Restful风格
method: @Controller @RequestMapping("/test") public String MyController{ @RequestMapping(& ...
- SpringMVC框架——集成RESTful架构
REST:Representational State Transfer 资源表现层状态转换 Resources 资源 Representation 资源表现层 State Transfer 状态转换 ...
- SpringMVC 框架系列之初识与入门实例
微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.SpringMVC 概述 (1). MVC:Model-View-Control Contr ...
- SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)
SpringMVC框架入门配置 IDEA下搭建Maven项目 这个不错哦 http://www.cnblogs.com/qixiaoyizhan/p/5819392.html
- SSM框架之SpringMVC(1)入门程序
SpringMVC(1) 1.三层架构和MVC 1.1. 三层架构 咱们开发服务器端程序,一般都基于两种形式,一种C/S架构程序,一种B/S架构程序 使用Java语言基本上都是开发B/S架构的程序,B ...
- SpringMvc框架 解决在RESTFUL接口后加任意 “.xxx” 绕过权限的问题
问题描述: 框架使用的是SpringMVC.SpringSecurity,在做权限拦截的时候发现一个问题,假设对请求路径/user/detail进行了权限拦截,在访问/user/detail.abc的 ...
- SpringMVC框架之第一篇
2.SpringMVC介绍 2.1.SpringMVC是什么 SpringMVC是Spring组织下的一个表现层框架.和Struts2一样.它是Spring框架组织下的一部分.我们可以从Spring的 ...
- SpringMvc核心流程以及入门案例的搭建
1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...
随机推荐
- 螺旋队列和hiho1525逃离迷宫3
我是真调不出错误了! hiho1525逃离迷宫3 #include <stdio.h> #include <stdlib.h> #include <math.h> ...
- python urllib和urllib3包使用(转载于)
urllib.request 1. 快速请求 2.模拟PC浏览器和手机浏览器 3.Cookie的使用 4.设置代理 urllib.error URLError HTTPError urllib.par ...
- OpenStack 镜像服务 Glance部署(六)
Glance介绍 创建虚拟机我们需要有glance的支持,因为glance是提供镜像的服务. Glance有两个比较重要的服务: Glance-api:接受云系统镜像的构建.删除.读取请求 Glanc ...
- Hadoop生态圈-Kafka的完全分布式部署
Hadoop生态圈-Kafka的完全分布式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要内容就是搭建Kafka完全分布式,它是在kafka本地模式(https:/ ...
- Docker CE的安装 与镜像加速
Docker CE 的安装与镜像加速 Docker CE是docker的开源版本 CENTOS 安装Docker CE 系统要求: 操作系统需要使用centos7() centos-extras库 必 ...
- 强悍的CSS工具组合:Blueprint, Sass, Compass
掌握CSS是每个Web开发者的基本要求,虽然CSS本身并不复杂,但怎样写出支持所有主流浏览器(特别是IE)的CSS,以及在大型网站中如何有序地组织好CSS结构却是一个相当棘手的问题.我更多的是一个开发 ...
- [机器学习&数据挖掘]机器学习实战决策树plotTree函数完全解析
在看机器学习实战时候,到第三章的对决策树画图的时候,有一段递归函数怎么都看不懂,因为以后想选这个方向为自己的职业导向,抱着精看的态度,对这本树进行地毯式扫描,所以就没跳过,一直卡了一天多,才差不多搞懂 ...
- CSUST 1506 ZZ的计算器 模拟题
题目描述:实现一个计算器,可以进行任意步的整数以内的加减乘除运算,运算符号只有+.-.*./,求出结果. 解题报告:一个可以说麻烦的模拟题,我们可以这样,输入以字符串的形式输入,然后将输入先做一遍预处 ...
- Ubuntu GNOME单击任务栏图标最小化设置
在Ubuntu GNOME的发行版中,桌面使用的是GNOME,GNOME可以像Windows那样有一个底部任务栏,在Ubuntu GNOME中它称为 dash to dock,如下图: Windows ...
- 如何使用optipng压缩png图片
OptiPNG – Google推荐的png图片无损压缩工具下载及使用教程 2014年08月24日 实用软件 暂无评论 optipng png图片无损压缩工具介绍: optipng png图片无损压缩 ...