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. ...
随机推荐
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- poj2398
题解: 计算几何入门题 对每个二分最近的在它右边的杆子 如何判断一个杆子在它右边呢 计算机判断这些要更善于利用点积和叉积 如果叉积为正代表在顺时针方向叉积为负在逆时针 发现要在struct里面重载运算 ...
- [转]简单三步,用 Python 发邮件
https://zhuanlan.zhihu.com/p/24180606 0. 前言 发送电子邮件是个很常见的开发需求.比如你写了个监控天气的脚本,发现第二天要下雨,或者网站上关注的某个商品降价了, ...
- jQuery滑动开关按钮效果
效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- python中super的理解(转)
原文地址:https://www.zhihu.com/question/20040039 针对你的问题,答案是可以,并没有区别.但是这题下的回答我感觉都不够好. 要谈论 super,首先我们应该无视 ...
- phpMyAdmin 安装教程全攻略
管理MYSQL数据库的最好工具是PHPmyAdmin,现在最新版本是phpMyAdmin 2.9.0.2,这是一个国际上开源的软件,一直在更新版本,你可以从 http://www.phpmyadmin ...
- python--pip出错
问题: 1.使用pip install时,出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None) ...
- 数学建模:1.概述& 监督学习--回归分析模型
数学建模概述 监督学习-回归分析(线性回归) 监督学习-分类分析(KNN最邻近分类) 非监督学习-聚类(PCA主成分分析& K-means聚类) 随机算法-蒙特卡洛算法 1.回归分析 在统计学 ...
- SVM—PK—BP:SVR(better)和BP两种方法比较且实现建筑物钢筋混凝土抗压强度预测—Jason niu
load concrete_data.mat n = randperm(size(attributes,2)); p_train = attributes(:,n(1:80))'; t_train = ...
- 比特币源码分析--C++11和boost库的应用
比特币源码分析--C++11和boost库的应用 我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...