编写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. 利用端口转发来访问virtualbox虚拟机centos6的jupyter notebook

    1.除了在virtualbox中设置常规的端口转发外,还需要在windows上打开cmd,输入ssh -N -f -L localhost:8888:localhost:8889 -p 22 root ...

  2. PHP 处理接口保证数据安全性

    原地址:http://blog.csdn.net/lhbeggar/article/details/46377653 php做APP接口时,如何保证接口的安全性? 1.当用户登录APP时,使用http ...

  3. stark组件之注册与路由系统(三)

    在文章stark组件前戏中已经提到过,django的注册功能是通过AdminSite的单例进行组册的,所以在这里也可以进行单例模式. class AdminSite(object): def __in ...

  4. C++ 赋值运算符重载

    类的定义 class Test{ int id; public: Test(int i): id(i){ cout << "obj_" << i <& ...

  5. PLSQLDeveloper安装与配置(详细图文)

    PLSQLDeveloper安装与配置(详细图文) 听语音 | 浏览:21912 | 更新:2016-10-24 17:12 1 2 3 4 5 6 7 分步阅读 在公司做项目时需要使用PLSQL D ...

  6. Leetcode 150.逆波兰表达式求值

    逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总 ...

  7. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  8. Bone Collector II(hdu 2639)

    题意:求01背包的第k最优值 输入:第一行为T,下面是T组数据,每组数据有n,m,k 代表n件物品,m容量,和题目要求的k,下一行是n个物品的价值,再一行是n个物品的体积 输出:T行答案 /* 类似于 ...

  9. AtCoder Grand Contest 020 D - Min Max Repetition

    q<=1000个询问,每次问a,b,c,d:f(a,b)表示含a个A,b个B的字符串中,连续A或连续B最小的串中,字典序最小的一个串,输出这个串的c到d位.a,b<=5e8,d-c+1&l ...

  10. SQL SERVER 自增字段相关问题

    SET IDENTITY_INSERT Data0048_TEST ON --给自增列赋值 DBCC CHECKIDENT(TableName) --查看某个表中的自增列当前的值 DBCC CHECK ...