REST风格的增删改查(2)
一、源码
1.页面
index.jsp
<a href="emps">List All Employee</a>
<br><br>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- SpringMVC 中没有遍历循环的标签,所以使用JSTL -->
<%@ 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>
<!--
1.使用RESTFUL风格的crud,其中需要将删除操作的请求方式改成DELETE,所以可以使用在web.xml文件中配置的Filter
将POST请求转化为DELETE请求,但是删除按钮是一个超链接,即GET请求,所以要使用JS处理GET请求。
2.SpringMVC处理静态资源,优雅的REST风格的资源URL不希望带.html或.do等后缀,若将DispatcherServlet请求映射
配置为/,则SpringMVC将捕获web容器的所有请求,包括静态资源的请求,SpringMVC会把他们当成普通请求处理,因找不到对应的处理器
将导致错误。
解决办法:在SpringMVC的配置文件中配置<mvc:default-servlet-handler/>的方式解决静资源的问题
-->
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$(".delete").click(function(){
var href = $(this).attr("href");
$("form").attr("action", href).submit();
return false;
});
})
</script>
</head>
<body>
<br>
<p align="center">员工信息管理</p>
<br>
<!-- 添加员工操作 -->
<p align="center">
<a href="emp">Add New Employee</a>
</p>
<!-- 将GET请求转化为POST请求 -->
<form action="" method="post">
<input type="hidden" name="_method" value="DELETE" />
</form>
<!-- 首先判断跳转过来之前,请求域中是否包含有数据 -->
<c:if test="${empty requestScope.employees}">
没有任何员工信息
</c:if>
<!-- 请求域中包含的有数据 -->
<c:if test="${!empty requestScope.employees}">
<table border="1" cellspacing="0" align="center">
<tr>
<th>ID</th>
<th>LastName</th>
<th>Email</th>
<th>Gender</th>
<th>Departmet</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>
</body>
</html>
input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<!-- 使用SpringMVC的表单标签 -->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ 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>
<!--
把表单提交的数据转换成Employee对象
-->
<form action="testConversionServiceConverer" method="POST">
<!-- lastname-email-gender-department.id,例如:GG-gg@163.com-0-105 -->
Employee:<input type="text" name="employee"/>
<input type="submit" value="Submit"/>
</form>
<br><br>
<!--
1.该页面用于添加记录操作,同时又用于修改操作
2.使用SpringMVC标签的好处:快速开发,方便表单值的回显
3.注意:可以通过modelAttribute属性指定绑定的模型属性,若没有指定该属性,则默认中request域对象中读取command表单的
Bean,如果该属性值也不存在,则会发生错误。
-->
<form:form action="${pageContext.request.contextPath }/emp" method="POST"
modelAttribute="employee">
<!-- 显示错误信息 -->
<form:errors path="*"></form:errors>
<br>
<%-- <form:input path=""/>, 其中path属性对应着HTML表单标签中的name属性 --%>
<!-- 判断是否有ID,因为这个页面是添加操作和修改操作一同使用的,所以使用有无ID来区别 -->
<c:if test="${employee.id == null }">
LastNmae:<form:input path="lastName"/>
<form:errors path="lastName"></form:errors>
<br>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<input type="hidden" name="_method" value="PUT"/>
<%-- 对于_method不能使用form:hidden标签,因为methodAttribute对应的bean中没有_method这个属性
<form:hidden path="_method" value="PUT"/>
--%>
</c:if>
Email:<form:input path="email"/>
<form:errors path="email"></form:errors>
<br>
<%
Map<String, String> genders = new HashMap();
genders.put("1","Male");
genders.put("0","Female");
request.setAttribute("genders", genders);
%>
Gender:<form:radiobuttons path="gender" items="${genders }"/>
<br>
<!--
临时添加一个出生日期的属性Birth,这时会遇见一些问题:
1.数据类型转换
2.数据类型格式化
3.数据校验
-->
Birth:<form:input path="birth"/>
<form:errors path="birth"></form:errors>
<br>
Salary:<form:input path="salary"/>
<br>
Department:<form:select path="department.id"
items="${departments }" itemLabel="departmentName" itemValue="id"></form:select>
<br>
<input type="submit" value="添加"/>
</form:form>
</p>
</body>
</html>
2.实体类
Employee.java
package com.springmvc.crud.entities;
import java.util.Date;
import javax.validation.constraints.Past;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
/**
* 实体类
*
*/
public class Employee {
private Integer id;
//校验lastname不为空
@NotEmpty
private String lastName;
@Email
private String email;
//1 male, 0 female
private Integer gender;
private Department department;
//校验日期为当前系统时间之前的时间
@Past
//对日期进行格式化,由字符串转化成yyyy-MM-dd
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
//格式化字符串1,234,567.8为1234567.8
@NumberFormat(pattern="#,###,###.#")
private Float salary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
public Employee(Integer id, String lastName, String email, Integer gender,
Department department) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
}
public Employee() {
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + ", department=" + department
+ ", birth=" + birth + ", salary=" + salary + "]";
}
}
Department.java
package com.springmvc.crud.entities;
/**
* 实体类
*/
public class Department {
private Integer id;
private String departmentName;
public Department() {
}
public Department(int i, String string) {
this.id = i;
this.departmentName = string;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName
+ "]";
}
}
3.dao类
EmployeeDao.java
package com.springmvc.crud.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.springmvc.crud.entities.Department;
import com.springmvc.crud.entities.Employee;
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
@Autowired
private DepartmentDao departmentDao;
//模拟数据库
static {
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 0, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 1, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
}
//定义初始化变量
private static Integer initId = 1006;
//添加一条记录
public void save(Employee employee){
//当员工的Id为空值时,就在1006的基础上加加,作用类似于数据库中主键的自增
if(employee.getId() == null){
employee.setId(initId++);
}
//设置employee中department的属性值
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
//向数据库中添加记录
employees.put(employee.getId(), employee);
}
//获取所有的记录
public Collection<Employee> getAll(){
return employees.values();
}
//获取一条记录
public Employee get(Integer id){
return employees.get(id);
}
//删除一条记录
public void delete(Integer id){
employees.remove(id);
}
}
DepartmentDao.java
package com.springmvc.crud.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.springmvc.crud.entities.Department;
@Repository
public class DepartmentDao {
private static Map<Integer, Department> departments = null;
//模拟数据库
static {
departments = new HashMap<Integer, Department>();
departments.put(101, new Department(101, "D-AA"));
departments.put(102, new Department(102, "D-BB"));
departments.put(103, new Department(103, "D-CC"));
departments.put(104, new Department(104, "D-DD"));
departments.put(105, new Department(105, "D-EE"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id){
return departments.get(id);
}
}
4.Handler
EmployeeHandler.java
package com.springmvc.crud.handlers;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.springmvc.crud.dao.DepartmentDao;
import com.springmvc.crud.dao.EmployeeDao;
import com.springmvc.crud.entities.Employee;
/**
* 控制跳转,并传递参数
*/
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
//因为要在页面上显示部门信息,所以引入DepartmentDao
@Autowired
private DepartmentDao departmentDao;
/**
* 1.控制跳转到查询所有员工信息的操作
* 当请求到这个方法,把查到员工的集合放到Map中,同时自动的放入到请求域中,跳转到目标页面之后就可以直接获取
* @param map
* @return
*/
@RequestMapping("/emps")
public String list(Map<String, Object> map){
map.put("employees", employeeDao.getAll());
return "list";
}
/**
* 2.控制跳转到添加一条记录的页面,因为要在页面显示部门信息,所以使用Map映射
* @param map
* @return
*/
@RequestMapping(value="/emp", method=RequestMethod.GET )
public String input(Map<String, Object> map){
//把查找到的部门信息放入到map中,同时放到请求域中
map.put("departments", departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
}
@RequestMapping(value="/emp", method=RequestMethod.POST)
public String save(@Valid Employee employee, BindingResult result,
Map<String, Object> map){
System.out.println("save:" + employee);
if(result.getErrorCount() > 0){
System.out.println("出错了!");
for(FieldError error : result.getFieldErrors()){
System.out.println(error.getField() + ":" + error.getDefaultMessage());
}
//若校验出错,则跳转到指定页面
map.put("departments", departmentDao.getDepartments());
return "input";
}
//执行保存操作
employeeDao.save(employee);
return "redirect:/emps";
}
/**
* 3.控制跳转到删除一条记录
* @param id
* @return
*/
@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
/**
* 4.控制跳转到修改一条记录页面
* @param id
* @param map
* @return
*/
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String input(@PathVariable("id") Integer id, Map<String, Object> map){
System.out.println("修改");
//根据ID,查到记录,用于回显
map.put("employee", employeeDao.get(id));
//所有的部门都放到map中
map.put("departments", departmentDao.getDepartments());
return "input";
}
@RequestMapping(value="/emp", method=RequestMethod.PUT)
public String update(Employee employee){
System.out.println("提交修改");
employeeDao.save(employee);
return "redirect:/emps";
}
@ModelAttribute
public void getEmployee(@RequestParam(value="id", required=false) Integer id,
Map<String, Object> map){
System.out.println("@ModelAttribute"+id);
if(id != null){
map.put("employee", employeeDao.get(id));
}
}
/**
* 功能:对lastName属性,不赋值
* @param binder
*/
/*@InitBinder
public void initBinder(WebDataBinder binder){
binder.setDisallowedFields("lastName");
}*/
}
5.配置文件
web.xml
<servlet>
<servlet-name>springDispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置把post请求转化成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>
springmvc.xml
<!-- 1.配置自动扫描的包 -->
<context:component-scan base-package="com.springmvc.crud"></context:component-scan>
<!-- 2.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 3.解决静态资源文件的问题 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
REST风格的增删改查(2)的更多相关文章
- nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- REST风格的增删改查(1)
一.RESTFUL风格的CRUD(增删改查)案例 1.需求: ①显示所有员工信息:URI:emps,请求方式:GET ②添加员工所有信息:显示添加页面:URI:emp,请求方式:GET, 添加员工信息 ...
- 进入全屏 nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- SpringMVC 之 RESTful 风格的增删改查
1. 视图和视图解析器 视图解析器 请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View 或 ModelMap 等类型的处理方法, SpringMVC 也会 ...
- 如何用Spring框架的<form:form>标签实现REST风格的增删改查操作
1.首先创建两个bean类,Employee(职工)和Department(部门),一个部门可以有多个职工 Employee类(属性:职工ID:id:姓名:lastName:邮箱:email:性别:g ...
- springmvc-实现增删改查
30. 尚硅谷_佟刚_SpringMVC_RESTRUL_CRUD_显示所有员工信息.avi现在需要使用restful风格实现增删改查,需要将post风格的请求转换成PUT 请求和DELETE 请求 ...
- Restful风格wcf调用2——增删改查
写在前面 上篇文章介绍如何将wcf项目,修改成restful风格的接口,并在上面提供了查询的功能,上篇文章中也感谢园友在评论中的提的建议,自己也思考了下,确实是那个道理.在urltemplate中,定 ...
- restful 架构风格的curd(增删改查)
restful架构 概念:REST指的是一组架构约束条件和原则,如果一个架构符合REST的约束条件和原则,就称之为RESTful架构. restful不是一个专门的技术,他是一个规范.规范就是写写代码 ...
- SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显
在eclipse中javaEE环境下: 这儿并没有连接数据库,而是将数据存放在map集合中: 将各种架包导入lib下... web.xml文件配置为 <?xml version="1. ...
随机推荐
- window.open跳过浏览器拦截
转自https://www.cnblogs.com/shizk/p/8458916.html $('#btn').click(function () { //打开一个不被拦截的新窗口 var newW ...
- alpha冲刺1/10
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:来自双十一的爱 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 Alpha版本的任务细分安排 leangoo ...
- redis centos 6.5 redis版本3.2.8安装过程
redis作为非关系数据库的典型应用,在庞大的数据通信处理有着自己强大的优势,今天也自己来开始学些redis. 以下每一个语句都是我执行的命令. 按照所查资料分析,需要tcl测试工具,这个在cento ...
- vsftp为不同用户设置不同的ftp的根目录
需求 要求ftp登录后的根目录是/var/test/,但是又不能影响其他用户的登录路径,因为有些程序是直接在根目录进行操作的,而没有目录切换的过程.操作过程新建用户 useradd test1user ...
- ELK 环境搭建4-Kafka + zookeeper
一.安装前准备 1.节点 192.168.30.41 192.168.30.42 192.168.30.43 2.操作系统: Centos7.5 3.安装包 a.java8: jdk-8u181-li ...
- 启动tomcat出现Removing obsolete files from server... Could not clean server of obsolete ……错误
在Eclipse启动tomcat出现"Removing obsolete files from server... Could not clean server of obsolete …… ...
- Spark-Unit2-Spark交互式命令行与SparkWordCount
一.Spark交互式命令行 启动脚本:spark-shell 先启动spark:./start-all.sh 本地模式启动命令:/bin/spark-shell 集群模式启动命令:/bin/spark ...
- TF之RNN:基于顺序的RNN分类案例对手写数字图片mnist数据集实现高精度预测—Jason niu
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- HDU6397
HDU6397用小于n的m个数组成k,求方案数mod 998244353如果没有n的限制,直接用隔板法求就可以因为m个数中可以为0,所以不妨先都放上一个1,转化成不能为0的m个数来凑k+m,即C(k+ ...
- unity3d俄罗斯方块源码教程+源码和程序下载
小时候,大家都应玩过或听说过<俄罗斯方块>,它是红白机,掌机等一些电子设备中最常见的一款游戏.而随着时代的发展,信息的进步,游戏画面从简单的黑白方块到彩色方块,游戏的玩法机制从最简单的消方 ...