码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC_RESTRUL_CRUD

时间:2018-03-10 01:30:03      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:jackson   lis   www.   views   resource   nts   test   --   meta   

编写POJO

  Departmet:

 1 package org.springmvc.curd.entity;
 2 
 3 public class Department {
 4 private int id;
 5 private String departmentName;
 6 public int getId() {
 7     return id;
 8 }
 9 public void setId(int id) {
10     this.id = id;
11 }
12 public String getDepartmentName() {
13     return departmentName;
14 }
15 public void setDepartmentName(String departmentName) {
16     this.departmentName = departmentName;
17 }
18 @Override
19 public String toString() {
20     return "Department [id=" + id + ", departmentName=" + departmentName + "]";
21 }
22 public Department() {
23 }
24 public Department(int id, String departmentName) {
25     super();
26     this.id = id;
27     this.departmentName = departmentName;
28 }
29 
30 }

  Employee:

 1 package org.springmvc.curd.entity;
 2 
 3 
 4 public class Employee {
 5 private Integer id;
 6 private String lastName;
 7 private String email;
 8 private Integer gender;
 9 private Department department = new Department();
10 
11 public Department getDepartment() {
12     return department;
13 }
14 public void setDepartment(Department department) {
15     this.department = department;
16 }
17 
18 public Integer getId() {
19     return id;
20 }
21 public void setId(Integer id) {
22     this.id = id;
23 }
24 public String getLastName() {
25     return lastName;
26 }
27 public void setLastName(String lastName) {
28     this.lastName = lastName;
29 }
30 public String getEmail() {
31     return email;
32 }
33 public void setEmail(String email) {
34     this.email = email;
35 }
36 
37 
38 public Integer getGender() {
39     return gender;
40 }
41 public void setGender(Integer gender) {
42     this.gender = gender;
43 }
44 @Override
45 public String toString() {
46     return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department="
47             + department + "]";
48 }
49 
50 public Employee(int id, String lastName, String email, int i, Department department) {
51     super();
52     this.id = id;
53     this.lastName = lastName;
54     this.email = email;
55     this.gender = i;
56     this.department = department;
57 }
58 public Employee() {
59 }
60 }

编写Dao:

  DepartmentDao:

 1 package org.springmvc.crud.dao;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import org.springframework.stereotype.Repository;
 8 import org.springmvc.curd.entity.Department;
 9 @Repository
10 public class DepartmentDao {
11 private static Map departments;      
12 static {
13     departments = new HashMap<Integer,Department>();
14     departments.put(101,new Department(101,"Depertment_101"));
15     departments.put(102,new Department(102,"Depertment_102"));
16     departments.put(103,new Department(103,"Depertment_103"));
17     departments.put(104,new Department(104,"Depertment_104"));
18     departments.put(105,new Department(105,"Depertment_105"));
19 }
20 public Collection<Department> getDepartments(){
21     return departments.values();
22 }
23 public Department getDepartment(Integer id) {
24     
25     return (Department) departments.get(id);
26 }
27 }

  EmployeeDao:

 1 package org.springmvc.crud.dao;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Repository;
 9 import org.springmvc.curd.entity.Department;
10 import org.springmvc.curd.entity.Employee;
11 
12 import com.sun.org.apache.regexp.internal.recompile;
13 import com.sun.org.apache.xml.internal.security.Init;
14 @Repository
15 public class EmployeeDao {
16 private static Map<Integer, Employee> employees;
17 private static Integer initId =1006;
18 @Autowired
19 private  DepartmentDao departmentDao;
20 static {
21     employees = new HashMap<>();
22     employees.put(1001, new Employee(1001, "employee_1001","1001@163.com", 1,new Department()));
23     employees.put(1002, new Employee(1002, "employee_1002","1001@163.com", 0,new Department()));
24     employees.put(1003, new Employee(1003, "employee_1003","1001@163.com", 1,new Department()));
25     employees.put(1004, new Employee(1004, "employee_1004","1001@163.com", 0,new Department()));
26     employees.put(1005, new Employee(1005, "employee_1005","1001@163.com", 1,new Department()));
27 }
28 public void save(Employee employee){
29     if (employee.getId()==null) {
30         employee.setId(initId++);
31     }
32     employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
33     employees.put(employee.getId(), employee);
34 }
35 public Collection<Employee> getAll(){
36     return employees.values();
37 }
38 public  Employee get(Integer id) {
39     return employees.get(id);
40 }
41 public void delete(Integer id) {    
42     employees.remove(id);
43 }
44 }
45  

编写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:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 <!-- 
10     springmvc 处理静态资源 
11     dispatcherServlet  设置默认拦截了所有请求 包括页面中对图片 ,css,js文件等的加载
12     而在dipatcherServlet中并没有配置有过相关映射处理
13     1、配置<mvc:default-servlet-handler/> 处理被映射过的url
14     2、配置<mvc:annotation-driven></mvc:annotation-driven>
15     
16  -->
17 <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
18 <script type="text/javascript">
19     /*    先将delete的url发送到form 在由form进行delete类型请求*/
20     $(function(){
21         $(".delete").click(function(){
22             var href = $(this).attr("href");
23             $("form").attr("action", href).submit();            
24             return false;
25         });
26     })
27 </script>
28 </head>
29 <body>
30     员工信息如下:
31     <c:if test="${empty requestScope.employees}">
32         没有任何员工信息
33     </c:if>
34     <c:if test="${!empty requestScope.employees}">
35 
36         <table border="1" cellpadding="10" cellspacing="0">
37             <tr>
38                 <th>ID</th>
39                 <th>LastName</th>
40                 <th>Gender</th>
41                 <th>Department</th>
42                 <th>Edit</th>
43                 <th>Delete</th>            
44             </tr>
45              <c:forEach items="${requestScope.employees}" var="emp" >
46                 <tr>
47                     <td>${emp.id}</td>
48                     <td>${emp.lastName}</td>
49                     <td>${emp.gender==0?‘female‘:‘male‘}</td>
50                     <td>${emp.department.departmentName}</td>
51                     <td><a href="">Edit</a></td>
52                     <td><a class="delete" href="emp/${emp.id}">Delete</a></td>
53                 
54                 </tr>
55             </c:forEach>
56         </table>
57             <br><br>
58     <a href="addEmployee">addEmployee</a>
59     <!-- 只能由post请求转化为Delete请求   而默认的超链接是get请求
60         只能编写表单指定为post在进行转化
61         表单要求: name="_method"    value="DELETE"
62     -->
63     <form action="" method="POST">
64         <input type="hidden" name="_method" value="DELETE"/>
65     </form>
66     </c:if>
67 </body>
68 </html>

  input.jsp:

 1 <%@page import="org.springmvc.curd.entity.Department"%>
 2 <%@page import="java.util.Map"%>
 3 <%@page import="java.util.HashMap"%>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
11 <title>Insert title here</title>
12 </head>
13 <body>
14 <!-- 使用form标签  更方便得进行表单值得回显-->
15     <form:form action="save" method="POST" modelAttribute="employee">
16         <!--path 属性对应 HTML 表单name  -->
17         lastName:<form:input path="lastName"/>
18         <br>
19         email:<form:input path="email"/>
20         <br>
21         <%  Map<String, String> genders = new HashMap<String, String>();
22             genders.put("0","female");
23             genders.put("1","male");
24             request.setAttribute("genders", genders);
25         %>
26         gender:<form:radiobuttons path="gender" items="${genders}" />
27         
28         <br>
29          department:<form:select path="department.id" 
30                  items="${departments}" 
31                  itemLabel="departmentName"
32                   itemValue="id" ></form:select> 
33 
34         <input type="submit" value="submit"> 
35     </form:form>
36 </body>
37 </html>

 编写SpringMVC.xml  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 9 <context:component-scan base-package="org.springmvc"></context:component-scan>
10 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11     <property name="prefix" value="/WEB-INF/views/"></property>
12     <property name="suffix" value=".jsp"></property>
13 </bean>
14     <mvc:default-servlet-handler/>
15     <mvc:annotation-driven></mvc:annotation-driven>
16 </beans>

编写Handler

 1 package org.springmvc.curd.handlers;
 2 
 3 import java.util.Map;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.PathVariable;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springmvc.crud.dao.DepartmentDao;
11 import org.springmvc.crud.dao.EmployeeDao;
12 import org.springmvc.curd.entity.Department;
13 import org.springmvc.curd.entity.Employee;
14 
15 import com.fasterxml.jackson.annotation.JsonFormat.Value;
16 import com.sun.org.apache.bcel.internal.generic.NEW;
17 import com.sun.org.apache.regexp.internal.recompile;
18 
19 @Controller
20 public class EmployeeHandler {
21 @Autowired
22 private EmployeeDao employeeDao;
23 @Autowired
24 private DepartmentDao departmentDao;
25     
26 @RequestMapping("emps")
27 public String list(Map<String,Object>map) {
28     map.put("employees",employeeDao.getAll());
29 return "list";    
30 }
31 @RequestMapping(value="addEmployee",method=RequestMethod.GET)
32 public String input(Map<String,Object> map) {
33     map.put("departments",departmentDao.getDepartments());
34     map.put("employee", new Employee());
35     return "input";
36 }
37 //@PathVariable可用于获取随url发送过来的变量 匹配到参数
38 @RequestMapping(value="save", method = RequestMethod.POST)
39 public String save(Employee employee) {
40     employeeDao.save(employee);
41     return "redirect:emps";
42 }
43 @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
44 public String delete(@PathVariable("id")Integer id) {
45     employeeDao.delete(id);
46 return "redirect:emps";    
47 }
48 }

 

SpringMVC_RESTRUL_CRUD

标签:jackson   lis   www.   views   resource   nts   test   --   meta   

原文地址:https://www.cnblogs.com/the-wang/p/8108208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!