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 ...
随机推荐
- 【CF437C】The Child and Toy
题目大意:给定一个有 N 个点,M 条边的无向图,点有点权,删除一个点就要付出所有与之有联系且没有被删除的点的点权之和的代价,求将所有点删除的最小代价是多少. 题解:从图连通性的角度出发,删除所有点就 ...
- linux socat命令
http://note.youdao.com/noteshare?id=35901183d9ccc09632339ec971fa58dd
- SQL Server 中ROW_NUMBER() OVER基本用法
1.不能排序法 * FROM table1 WHERE id NOT IN ( SELECT TOP 开始的位置 id FROM table1 ) 2.SQL 2000 临时表法 DECLARE @S ...
- bzoj千题计划215:bzoj1047: [HAOI2007]理想的正方形
http://www.lydsy.com/JudgeOnline/problem.php?id=1047 先用单调队列求出每横着n个最大值 再在里面用单调队列求出每竖着n个的最大值 这样一个位置就代表 ...
- dede调用多级导航的方法
<div id="navWrapper"> <div class="content"> <ul class="nav m ...
- iOS 提交应用过程出现的错误及#解决方案#images can't contain alpha channels or transparencies
本文永久地址为http://www.cnblogs.com/ChenYilong/p/3977542.html ,转载请注明出处. 当你试图通过<预览>"导出&qu ...
- 蓝桥杯 问题 1117: K-进制数 (递归)
题目链接 题目描述 考虑包含N位数字的K-进制数. 定义一个数有效, 如果其K-进制表示不包含两连续的0. 考虑包含N位数字的K-进制数. 定义一个数有效, 如果其K-进制表示不包含两连续的0. 例: ...
- 洛谷 P1006 传纸条 多维DP
传纸条详解: 蒟蒻最近接到了练习DP的通知,于是跑来试炼场看看:发现有点难(毕竟是蒟蒻吗)便去翻了翻题解,可怎么都看不懂.为什么呢?蒟蒻发现题解里都非常详细的讲了转移方程,讲了降维优化,但这题新颖之处 ...
- 【ORACLE】创建表空间
CREATE TABLESPACE dna36 DATAFILE 'D:\oracle\oradata\orcl\dna36.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M ...
- 2017/05/20 java 基础 随笔
static 关键字的特点 1.随着类的加载而加载 2.优先于对象存在 3.被类的所有对象共享 如果某个成员变量是被所有对象共享的,那么他就应该定义为静态的 4.可以通过类名调用 其实它本身也可以通过 ...