编写POJO

  Departmet:

 package org.springmvc.curd.entity;

 public class Department {
private int id;
private String departmentName;
public int getId() {
return id;
}
public void setId(int 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 + "]";
}
public Department() {
}
public Department(int id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
} }

  Employee:

 package org.springmvc.curd.entity;

 public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department = new Department(); public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
} 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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department="
+ department + "]";
} public Employee(int id, String lastName, String email, int i, Department department) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = i;
this.department = department;
}
public Employee() {
}
}

编写Dao:

  DepartmentDao:

 package org.springmvc.crud.dao;

 import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Repository;
import org.springmvc.curd.entity.Department;
@Repository
public class DepartmentDao {
private static Map departments;
static {
departments = new HashMap<Integer,Department>();
departments.put(101,new Department(101,"Depertment_101"));
departments.put(102,new Department(102,"Depertment_102"));
departments.put(103,new Department(103,"Depertment_103"));
departments.put(104,new Department(104,"Depertment_104"));
departments.put(105,new Department(105,"Depertment_105"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id) { return (Department) departments.get(id);
}
}

  EmployeeDao:

 package org.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 org.springmvc.curd.entity.Department;
import org.springmvc.curd.entity.Employee; import com.sun.org.apache.regexp.internal.recompile;
import com.sun.org.apache.xml.internal.security.Init;
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees;
private static Integer initId =1006;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<>();
employees.put(1001, new Employee(1001, "employee_1001","1001@163.com", 1,new Department()));
employees.put(1002, new Employee(1002, "employee_1002","1001@163.com", 0,new Department()));
employees.put(1003, new Employee(1003, "employee_1003","1001@163.com", 1,new Department()));
employees.put(1004, new Employee(1004, "employee_1004","1001@163.com", 0,new Department()));
employees.put(1005, new Employee(1005, "employee_1005","1001@163.com", 1,new Department()));
}
public void save(Employee employee){
if (employee.getId()==null) {
employee.setId(initId++);
}
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);
}
}

编写jsp:

  index.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>
<a href="emps" >list all emps</a> </body>
</html>

  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>
<!--
springmvc 处理静态资源
dispatcherServlet 设置默认拦截了所有请求 包括页面中对图片 ,css,js文件等的加载
而在dipatcherServlet中并没有配置有过相关映射处理
1、配置<mvc:default-servlet-handler/> 处理被映射过的url
2、配置<mvc:annotation-driven></mvc:annotation-driven> -->
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
/* 先将delete的url发送到form 在由form进行delete类型请求*/
$(function(){
$(".delete").click(function(){
var href = $(this).attr("href");
$("form").attr("action", href).submit();
return false;
});
})
</script>
</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>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.gender==0?'female':'male'}</td>
<td>${emp.department.departmentName}</td>
<td><a href="">Edit</a></td>
<td><a class="delete" href="emp/${emp.id}">Delete</a></td> </tr>
</c:forEach>
</table>
<br><br>
<a href="addEmployee">addEmployee</a>
<!-- 只能由post请求转化为Delete请求 而默认的超链接是get请求
只能编写表单指定为post在进行转化
表单要求: name="_method" value="DELETE"
-->
<form action="" method="POST">
<input type="hidden" name="_method" value="DELETE"/>
</form>
</c:if>
</body>
</html>

  input.jsp:

 <%@page import="org.springmvc.curd.entity.Department"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@ 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:form action="save" method="POST" modelAttribute="employee">
<!--path 属性对应 HTML 表单name -->
lastName:<form:input path="lastName"/>
<br>
email:<form:input path="email"/>
<br>
<% Map<String, String> genders = new HashMap<String, String>();
genders.put("0","female");
genders.put("1","male");
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> <input type="submit" value="submit">
</form:form>
</body>
</html>

编写SpringMVC.xml  

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="org.springmvc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>

编写Handler

 package org.springmvc.curd.handlers;

 import java.util.Map;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springmvc.crud.dao.DepartmentDao;
import org.springmvc.crud.dao.EmployeeDao;
import org.springmvc.curd.entity.Department;
import org.springmvc.curd.entity.Employee; import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.regexp.internal.recompile; @Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao; @RequestMapping("emps")
public String list(Map<String,Object>map) {
map.put("employees",employeeDao.getAll());
return "list";
}
@RequestMapping(value="addEmployee",method=RequestMethod.GET)
public String input(Map<String,Object> map) {
map.put("departments",departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
}
//@PathVariable可用于获取随url发送过来的变量 匹配到参数
@RequestMapping(value="save", method = RequestMethod.POST)
public String save(Employee employee) {
employeeDao.save(employee);
return "redirect:emps";
}
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable("id")Integer id) {
employeeDao.delete(id);
return "redirect:emps";
}
}

SpringMVC_RESTRUL_CRUD的更多相关文章

随机推荐

  1. PHP:Mysql 基础类

    文章来源:http://www.cnblogs.com/hello-tl/p/7592547.html <?php /** * __construct($Mysql_config) 构造函数 $ ...

  2. pycharm中提交Git 忽略部分代码

    痛点: 项目中,有些配置项,或者比较隐私的东东,不想上传 解决:在项目根路径下,创建.gitignore 文件     文件中可以写文件名.文件路径等 结果: 提交到git,发现果真没有dbconne ...

  3. vim 编辑器使用法则

    vim 编辑器使用法则 Vi编辑器有3种使用模式:一般模式.编辑模式和命令模式. $SHELL:查看当前默认shell类型  $BASH_VERSION:查看当前shell版本 3.一般模式: 光标移 ...

  4. 九度oj 题目1064:反序数

    题目1064:反序数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5163 解决:3698 题目描述: 设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321)求N的 ...

  5. 【bzoj3505】[Cqoi2014]数三角形

    [bzoj3505][Cqoi2014]数三角形 2014年5月15日3,5230 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4×4的网格上的一个三角 ...

  6. 【Intellij】Intellij Idea 2017创建web项目及tomcat部署实战

    相关软件:Intellij Idea2017.jdk16.tomcat7 Intellij Idea直接安装(可根据需要选择自己设置的安装目录),jdk使用1.6/1.7/1.8都可以,主要是配置好系 ...

  7. SOJ 3300_Stockholm Coins

    [题意]给n个数,求一个数,使这个数能且只能由(n个数每个至少出现一次)表示.输出满足条件的最小的数. [分析](完全背包)如果有满足条件的最小的数,那么这个数只能是这n个数的和total,通过记录每 ...

  8. 常用的delphi 第三方控件

    Devexpress VCL 这个基本上覆盖了系统界面及数据库展示的方方面面,是做桌面系统必备的一套控件,目前的版本是2011.2.3, 支持win32 及win64. AutoUpgrader 这个 ...

  9. csu1364 Interview

    对拍了一波才找到的错误,此题我用的是二分答案加倍增查询,实际上query那里我觉得仍然有缺陷,因为每一次我的查找还是在循环找到一个k使得x+2^k <= y,而错的地方也正在此地,一开始没有判断 ...

  10. HDU——1150 Machine Schedule

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...