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 ...
随机推荐
- 使用solrJ操作solr常用方法 【注释非常详细,非常好】
转: 使用solrJ操作solr常用方法 2017年08月07日 22:49:06 成都往右 阅读数:8990 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- linux ------ 硬连接和软连接(软连接也叫符号连接)
在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号 (Inode Index).在Linux中,多个文件名指向同一索引节点是存在的.一般这种连接就是硬连接 ...
- 模拟生成环境的MySQL安装方法-通用二进制方式安装
模拟生成环境的MySQL安装方法-通用二进制方式安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.并发响应用户请求的网络IO模型 1>.单进程 特点:一个进程响应一个请 ...
- sql数据库连接字符串在APP.config配置文件内的两种写法
第一种方法 写法: <configuration> <appSettings> <add key="connectionstring" value=& ...
- Apache POI - Excel
基于模板的EXCEL报表组件ExcelUtils:http://blog.csdn.net/hanqunfeng/article/details/4834875 http://blog.csdn.ne ...
- OpenStack中RabbitMQ高可用性配置
采用镜像队列的方案进行配置 1. 网络拓扑 node1:10.10.11.1 node2:10.10.11.2 2. 配置hosts node1+node2: vim /etc/hosts >1 ...
- HDU 3389 阶梯博弈变形
n堆石子,每次选取两堆a!=b,(a+b)%2=1 && a!=b && 3|a+b,不能操作者输 选石子堆为奇数的等价于选取步数为奇数的,观察发现 1 3 4 是无法 ...
- HDU 3094 树上删边 NIM变形
基本的树上删边游戏 写过很多遍了 /** @Date : 2017-10-13 18:19:37 * @FileName: HDU 3094 树上删边 NIM变形.cpp * @Platform: W ...
- 并查集:POJ 1182 食物链 复习
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...
- inline-block的间距问题
张鑫旭的博客有提到,解决的方法有很多,先贴下,回头再做整理. http://www.zhangxinxu.com/wordpress/2012/04/inline-block-space-remove ...