学习SpringMVC——你们要的REST风格的CRUD来了
来源:互联网 发布:sysbench mysql 测试 编辑:程序博客网 时间:2024/05/19 07:09
如果说前面是准备调料,洗菜,切菜,摆盘,那么今天就来完整的上道菜,主要说的是基于REST风格实现数据的增删改查(CRUD)操作。
用料
entity:Employee + Department
对于这样的实体类大家肯定很熟悉,一个Employee就是一个员工,一个Department就是一个部门,一个员工只属于一个部门,一个部门可以对应多个员工。
Employee.java
Department.java
dao层:除了entity层,我们还需要有dao层,用于对于entity的增删改查的操作,这里对应的有EmployeeDao和DepartmentDao下载
EmployeeDao.java
该dao提供了:1. 添加保存一个employee——save方法
2. 获取所有员工信息——getAll方法
3. 根据指定员工id查找员工——get方法
4. 根据指定员工id删除员工——delete方法
DepartmentDao.java下载
该dao提供了:1. 查找所有部门信息——getDepartments
2. 根据指定id查找相应的部门信息——getDepartment
另外这里需要用到的所有jar包都在这里下载
用料齐活了, 就开始做菜了,要做的分别是REST清蒸C(添加记录)、REST油炸R(查询记录)、REST红烧U(更新记录)和REST水煮D(删除记录)。
1. 查询记录
这个很简单,跟前几篇介绍的套路差不多,新建一个index.jsp,放入一个用于查询的超链接下载
<
a
href="emps">list all employees</
a
><
br
/>
相应的在Handler中需要添加映射支持,这里新建一个EmployeeHandler类
package
com.jackie.springmvc.crud.handlers;
import
java.util.Map;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
com.jackie.springmvc.crud.dao.EmployeeDao;
@Controller
public
class
EmployeeHandler {
@Autowired
private
EmployeeDao employeeDao;
@RequestMapping
(
"/emps"
)
public
String list(Map<String, Object> map){
map.put(
"employees"
, employeeDao.getAll());
return
"list"
;
}
}
注意:
这里RequestMapping中没有指明RequestMethod的请求类型,默认是GET方式。下载
最终我们需要将查询的数据解析出来放到页面上,这个页面就是list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!
DOCTYPE
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">
<
title
>Insert title here</
title
>
</
head
>
<
body
>
<
c:if
test="${empty requestScope.employees}">
没有员工信息
</
c:if
>
<
c:if
test="${!empty requestScope.employees }">
<
table
border="1" cellpadding="10" cellspacing="0">
<
tr
>
<
th
>ID</
th
>
<
th
>LastName</
th
>
<
th
>Email</
th
>
<
th
>Gender</
th
>
<
th
>Department</
th
>
<
th
>Edit</
th
>
<
th
>Delete</
th
>
</
tr
>
<
c:forEach
items="${requestScope.employees }" var="emp">
<
tr
>
<
td
>${emp.id }</
td
>
<
td
>${emp.lastName }</
td
>
<
td
>${emp.email }</
td
>
<
td
>${emp.gender==0?'Female':'Male' }</
td
>
<
td
>${emp.department.departmentName }</
td
>
<
td
><
a
href="emp/${emp.id}">Edit</
a
></
td
>
<
td
><
a
class="delete" href="emp/${emp.id}">Delete</
a
></
td
>
</
tr
>
</
c:forEach
>
</
table
>
</
c:if
>
<
br
><
br
>
</
body
>
</
html
>
这里通过表格的方式显示,表格的每一行显示一个员工记录。
如果Handler层没有传入employee,则显示“没有员工信息”。
如果从Handler层传入查询到的employee的信息,则通过遍历的方式查询每条记录并显示在对应的表格中。
结果如下:下载
这里查询了所有的员工信息,为简便起见,这里的员工信息并不是从数据库中读取的,而是在EmployeeDao中通过静态模块声明构造的,将每个员工信息放入一个map集合中。
2. 添加记录
添加记录即在原有的数据集合基础上,再新建一个员工信息,我们需要为员工的每个属性填充值,所以我们需要一个页面来为这些字段赋值,这个页面就叫input.jsp。
添加员工信息的入口链接就放在list.jsp中
<
a
href = "emp">add new employee</
a
><
br
/>
同时在EmployeeHandler中添加映射下载
@RequestMapping
(value=
"/emp"
, method=RequestMethod.GET)
public
String input(Map<String, Object> map){
map.put(
"departments"
, departmentDao.getDepartments());
map.put(
"employee"
,
new
Employee());
return
"input"
;
}
input.jsp
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!
DOCTYPE
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">
<
title
>Insert title here</
title
>
</
head
>
<
body
>
<
form:form
action="emp" method="POST" modelAttribute="employee">
<!-- path 属性对应html表单标签的name属性 -->
LastName: <
form:input
path="lastName"></
form:input
><
br
>
Email:<
form:input
path="email"></
form:input
><
br
>
<%
Map<
String
, String> genders = new HashMap<
String
, String>();
genders.put("1", "male");
genders.put("0", "female");
request.setAttribute("genders", genders);
%>
Gender:<
form:radiobuttons
path="gender" items="${genders }"/><
br
>
Department:<
form:select
path="department.id" items="${departments }" itemLabel="departmentName" itemValue="id"></
form:select
><
br
>
<
input
type="submit" value="Submit"/>
</
form:form
>
</
body
>
</
html
>
- 学习SpringMVC——你们要的REST风格的CRUD来了
- springmvc Rest 风格的 CRUD
- SpringMVC学习笔记-REST风格CRUD
- 使用SpringMVC实现REST风格的CRUD功能
- spring mvc rest风格的CRUD
- REST风格编写Spring MVC的CRUD
- SpringMVC的REST风格开发
- SpringMVC REST实现入门级的CRUD
- SpringMVC REST实现入门级的CRUD
- 【知识整理】SpringMVC-REST风格CRUD
- springmvc的Rest风格的基本写法
- 使用springMVC构建REST风格的应用
- SpringMVC构建REST风格的URI
- springMVC------Rest风格,HiddenHttpMethodFilter的作用
- springmvc使用rest风格的404错误
- springMVC下的REST风格【基础】
- 使用springMVC创建REST风格的服务器
- SpringMVC联手REST实现入门级的CRUD
- C中结构体struct的用法
- 在Java中使用HttpURLConnection发送http客户端请求、服务器端返回信息、接受服务器端的信息
- 收藏
- 利用FFmpeg进行视频文件进行分割
- Android攻城狮—全套必备神级工具(开发,插件,效率)
- 学习SpringMVC——你们要的REST风格的CRUD来了
- ionic 处理ios侧滑白屏
- Android 自定义View (三) 圆环交替 等待效果
- 值类型与引用类型
- SpringMVC框架下,通过jQuery发送ajax异步(asynchronous)请求
- 当修改网站上的图片等资源时怎样避免客户缓存的问题
- 友盟社会化分享遇到的问题
- 蓝鸥Unity开发教程之课时4 3D开发基础
- java web中的绝对路径