SpringMVC中CRUD实例

一:新建项目(下面的几乎属于公共的方法,不需要改动)

1.结构

  SpringMVC中CRUD实例_第1张图片

 

  

2.添加lib

 

3.配置web.xml

 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>SpringMvcCRUDdisplay-name>
 4   
 5     <servlet>
 6         <servlet-name>DispatchServletservlet-name>
 7         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
 8         <init-param>
 9             <param-name>contextConfigLocationparam-name>
10             <param-value>classpath:springmcv.xmlparam-value>
11         init-param> 
12         <load-on-startup>1load-on-startup>
13     servlet>
14     
15     <servlet-mapping>
16         <servlet-name>DispatchServletservlet-name>
17         <url-pattern>/url-pattern>
18     servlet-mapping>
19     
20     
21     <filter>
22         <filter-name>HiddenHttpMethodFilterfilter-name>
23         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
24     filter>
25     <filter-mapping>
26         <filter-name>HiddenHttpMethodFilterfilter-name>
27         <url-pattern>/*url-pattern>
28     filter-mapping>
29 web-app>

 

4.配置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 
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context 
 9         http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12                    
13     <context:component-scan base-package="com.spring.it" >context:component-scan>
14     
15     
16     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17         <property name="prefix" value="/WEB-INF/views/" />
18           <property name="suffix" value=".jsp" />
19     bean>    
20 beans>

 

5.实体类Department

 1 package com.spring.it.enties;
 2 
 3 public class Department {
 4 
 5     private Integer id;
 6     private String departmentName;
 7 
 8     public Department() {
 9         
10     }
11     
12     public Department(int i, String string) {
13         this.id = i;
14         this.departmentName = string;
15     }
16 
17     public Integer getId() {
18         return id;
19     }
20 
21     public void setId(Integer id) {
22         this.id = id;
23     }
24 
25     public String getDepartmentName() {
26         return departmentName;
27     }
28 
29     public void setDepartmentName(String departmentName) {
30         this.departmentName = departmentName;
31     }
32 
33     @Override
34     public String toString() {
35         return "Department [id=" + id + ", departmentName=" + departmentName
36                 + "]";
37     }
38     
39 }

 

6.实体类Employee

 1 package com.spring.it.enties;
 2 
 3 import java.util.Date;
 4 import org.springframework.format.annotation.DateTimeFormat;
 5 import org.springframework.format.annotation.NumberFormat;
 6 
 7 public class Employee {
 8 
 9     private Integer id;
10     private String lastName;
11     private String email;
12     //1 male, 0 female
13     private Integer gender;
14     private Department department;
15     
16     public Employee(Integer id, String lastName, String email, Integer gender,
17             Department department) {
18         super();
19         this.id = id;
20         this.lastName = lastName;
21         this.email = email;
22         this.gender = gender;
23         this.department = department;
24     }
25 
26     public Employee() {
27         
28     }
29     public Integer getId() {
30         return id;
31     }
32 
33     public void setId(Integer id) {
34         this.id = id;
35     }
36 
37     public String getLastName() {
38         return lastName;
39     }
40 
41     public void setLastName(String lastName) {
42         this.lastName = lastName;
43     }
44 
45     public String getEmail() {
46         return email;
47     }
48 
49     public void setEmail(String email) {
50         this.email = email;
51     }
52 
53     public Integer getGender() {
54         return gender;
55     }
56 
57     public void setGender(Integer gender) {
58         this.gender = gender;
59     }
60 
61     public Department getDepartment() {
62         return department;
63     }
64 
65     public void setDepartment(Department department) {
66         this.department = department;
67     }
68     
69     @Override
70     public String toString() {
71         return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender
72                 + ", department=" + department + "]";
73     }
74 
75     
76 }

 

7.Dao---DepartmentDao

 1 package com.spring.it.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 com.spring.it.enties.Department;
 9 
10 @Repository
11 public class DepartmentDao {
12 
13     private static Map departments = null;
14     
15     static{
16         departments = new HashMap();
17         
18         departments.put(101, new Department(101, "D-AA"));
19         departments.put(102, new Department(102, "D-BB"));
20         departments.put(103, new Department(103, "D-CC"));
21         departments.put(104, new Department(104, "D-DD"));
22         departments.put(105, new Department(105, "D-EE"));
23     }
24     
25     public Collection getDepartments(){
26         return departments.values();
27     }
28     
29     public Department getDepartment(Integer id){
30         return departments.get(id);
31     }
32     
33 }

 

8.EmployeeDao

 1 package com.spring.it.dao;
 2 
 3 import com.spring.it.enties.Department;
 4 import java.util.Collection;
 5 import java.util.HashMap;
 6 import java.util.Map;
 7 
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Repository;
10 import com.spring.it.enties.Employee;
11 @Repository
12 public class EmployeeDao {
13 
14     private static Map employees = null;
15     
16     @Autowired
17     private DepartmentDao departmentDao;
18     
19     static{
20         employees = new HashMap();
21 
22         employees.put(1001, new Employee(1001, "E-AA", "[email protected]", 1, new Department(101, "D-AA")));
23         employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1, new Department(102, "D-BB")));
24         employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0, new Department(103, "D-CC")));
25         employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0, new Department(104, "D-DD")));
26         employees.put(1005, new Employee(1005, "E-EE", "[email protected]", 1, new Department(105, "D-EE")));
27     }
28     
29     private static Integer initId = 1006;
30     
31     public void save(Employee employee){
32         if(employee.getId() == null){
33             employee.setId(initId++);
34         }
35         
36         employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
37         employees.put(employee.getId(), employee);
38     }
39     
40     public Collection getAll(){
41         return employees.values();
42     }
43     
44     public Employee get(Integer id){
45         return employees.get(id);
46     }
47     
48     public void delete(Integer id){
49         employees.remove(id);
50     }
51 }

 

二:展示所有的员工---查看操作

1.Controller--EmployeeHander

 1 package com.spring.it.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.RequestMapping;
 8 
 9 import com.spring.it.dao.EmployeeDao;
10 
11 @Controller
12 public class EmployeeHander {
13     @Autowired(required=true)
14     private EmployeeDao employeeDao;
15     
16     @RequestMapping("/emps")
17     public String list(Map map) {
18         System.out.println("====");
19         map.put("employee", employeeDao.getAll());
20         return "list";
21     }
22 }

 

2.首页index.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title heretitle>
 8 head>
 9 <body>
10     <br>
11     <a href="emps">list empsa>
12 body>
13 html>

 

3.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 heretitle>
 9 head>
10 <body>
11     This Is All Employee
12     <c:if test="${empty requestScope.employee }">
13         没有任何的员工信息
14     c:if>
15     <c:if test="${!empty requestScope.employee }">
16         <table border="1" cellpadding="10" cellspacing="0">
17             <tr>            
18                 <th>IDth>
19                 <th>LastNameth>
20                 <th>Emailth>
21                 <th>Genderth>
22                 <th>Departmentth>
23                 <th>Editth>
24                 <th>Deleteth>
25             tr>
26             <c:forEach items="${requestScope.employee }" var="emp">
27                 <tr>
28                     <td>${emp.id}td>
29                     <td>${emp.lastName}td>
30                     <td>${emp.email}td>
31                     <td>${emp.gender==0?'Female':'Male'}td>
32                     <td>${emp.department.departmentName}td>
33                     <td><a href="">Edita>td>
34                     <td><a href="">Deletea>td>
35                 tr>
36             c:forEach>
37         table>
38     c:if>
39 body>
40 html>

 

4.效果

  SpringMVC中CRUD实例_第2张图片

 

三:添加操作

1.controller

  主要是增加了input与save操作。

 1 package com.spring.it.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.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 
10 import com.spring.it.dao.DepartmentDao;
11 import com.spring.it.dao.EmployeeDao;
12 import com.spring.it.enties.Department;
13 import com.spring.it.enties.Employee;
14 
15 @Controller
16 public class EmployeeHander {
17     @Autowired(required=true)
18     private EmployeeDao employeeDao;
19     
20     @Autowired(required=true)
21     private DepartmentDao departmentDao;
22     
23     /**
24      * 保存,是submit提交过来的请求,属于Post请求
25      */
26     @RequestMapping(value="/emp",method=RequestMethod.POST)
27     public String save(Employee employee) {
28         employeeDao.save(employee);
29         return "redirect:/emps";
30     }
31     
32     /**
33      * 跳转到input页面,用于添加员工,是Get请求
34      */
35     @RequestMapping(value="/emp",method=RequestMethod.GET)
36     public String input(Map map) {
37         map.put("department", departmentDao.getDepartments());
38         //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值
39         map.put("employee", new Employee());
40         return "input";
41     }
42     
43     /**
44      * 展示所有的员工
45      */
46     @RequestMapping("/emps")
47     public String list(Map map) {
48         System.out.println("====");
49         map.put("employee", employeeDao.getAll());
50         return "list";
51     }
52 }

 

2.input.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
 4 <%@ page import="java.util.Map"%>
 5 <%@ page import="java.util.HashMap"%>
 6 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
10 <title>Insert title heretitle>
11 head>
12 <body>
13     
14     
15     <form:form action="emp" method="POST" modelAttribute="employee">
16         LastName:<form:input path="lastName"/><br>
17         Email:<form:input path="email"/><br>
18         <%
19             Map<String,String> genders=new HashMap();
20             genders.put("1", "Male");
21             genders.put("0", "Female");
22             request.setAttribute("genders", genders);
23         %>
24         Gender:<form:radiobuttons path="gender" items="${genders}"/><br>
25         Department:<form:select path="department.id" 
26                         items="${department}" itemLabel="departmentName" itemValue="id">form:select><br>
27         <input type="submit" values="Submit">
28     form:form>
29 body>
30 html>

 

3.效果

  SpringMVC中CRUD实例_第3张图片

  SpringMVC中CRUD实例_第4张图片

 

4.PS---form表单

  使用的是spring form表单,在input中需要引入标签。

  SpringMVC中CRUD实例_第5张图片

  SpringMVC中CRUD实例_第6张图片

 

四:删除操作

1.修改list.jsp

  因为这个时候的list.jsp的delete按钮的连接还是空的,需要补充进去。

  这个get不能直接转换成delete操作,所以需要借助js。

 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 heretitle>
 9 <script type="text/javascript" src="scripts/jquery-1.9.1.min.js">script>
10 <script type="text/javascript">
11     $(function(){
12         $(".delete").click(function(){
13             var href = $(this).attr("href");
14             $("form").attr("action", href).submit();    
15             return false;
16         })
17     })
18 script>
19 head>
20 <body>
21     <form action="" method="POST">
22         <input type="hidden" name="_method" value="DELETE">
23     form>
24 
25     This Is All Employee
26     <c:if test="${empty requestScope.employee }">
27         没有任何的员工信息
28     c:if>
29     <c:if test="${!empty requestScope.employee }">
30         <table border="1" cellpadding="10" cellspacing="0">
31             <tr>            
32                 <th>IDth>
33                 <th>LastNameth>
34                 <th>Emailth>
35                 <th>Genderth>
36                 <th>Departmentth>
37                 <th>Editth>
38                 <th>Deleteth>
39             tr>
40             <c:forEach items="${requestScope.employee }" var="emp">
41                 <tr>
42                     <td>${emp.id}td>
43                     <td>${emp.lastName}td>
44                     <td>${emp.email}td>
45                     <td>${emp.gender==0?'Female':'Male'}td>
46                     <td>${emp.department.departmentName}td>
47                     <td><a href="">Edita>td>
48                     <td><a href="emp/${emp.id}" class="delete">Deletea>td>
49                 tr>
50             c:forEach>
51         table>
52     c:if>
53     <br><br>
54     <a href="emp">Add New Employeea>
55 body>
56 html>

 

2.controller

 1 package com.spring.it.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.springframework.web.bind.annotation.ResponseBody;
11 
12 import com.spring.it.dao.DepartmentDao;
13 import com.spring.it.dao.EmployeeDao;
14 import com.spring.it.enties.Department;
15 import com.spring.it.enties.Employee;
16 
17 @Controller
18 public class EmployeeHander {
19     @Autowired(required=true)
20     private EmployeeDao employeeDao;
21     
22     @Autowired(required=true)
23     private DepartmentDao departmentDao;
24     
25     /**
26      * 删除操作
27      */
28     @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
29     @ResponseBody
30     public String delete(@PathVariable("id") Integer id) {
31         employeeDao.delete(id);
32         return "redirect:/emps";
33     }
34     
35     /**
36      * 保存,是submit提交过来的请求,属于Post请求
37      */
38     @RequestMapping(value="/emp",method=RequestMethod.POST)
39     public String save(Employee employee) {
40         employeeDao.save(employee);
41         return "redirect:/emps";
42     }
43     
44     /**
45      * 跳转到input页面,用于添加员工,是Get请求
46      */
47     @RequestMapping(value="/emp",method=RequestMethod.GET)
48     public String input(Map map) {
49         map.put("department", departmentDao.getDepartments());
50         //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值
51         map.put("employee", new Employee());
52         return "input";
53     }
54     
55     /**
56      * 展示所有的员工
57      */
58     @RequestMapping("/emps")
59     public String list(Map map) {
60         System.out.println("====");
61         map.put("employee", employeeDao.getAll());
62         return "list";
63     }
64 }

 

3.处理静态资源

  因为springmvc拦截所有的请求,所以静态资源也被拦截,但是静态资源没有被映射。

  但是静态资源是不需要映射的。

  解决方式是在springmvc的配置文件中配置default-servlet-handler,但是以前的功能又出现了问题,这个时候需要添加上annotation-driven

 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 
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context 
 9         http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12                    
13     <context:component-scan base-package="com.spring.it" >context:component-scan>
14     
15     
16     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17         <property name="prefix" value="/WEB-INF/views/" />
18           <property name="suffix" value=".jsp" />
19     bean>    
20     
21     <mvc:default-servlet-handler/>
22     <mvc:annotation-driven>mvc:annotation-driven>
23 beans>

 

4.效果

  SpringMVC中CRUD实例_第7张图片

 

五:修改

1.修改list

  Edit的连接需要修改。

 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 heretitle>
 9 <script type="text/javascript" src="scripts/jquery-1.9.1.min.js">script>
10 <script type="text/javascript">
11     $(function(){
12         $(".delete").click(function(){
13             var href = $(this).attr("href");
14             $("form").attr("action", href).submit();    
15             return false;
16         })
17     })
18 script>
19 head>
20 <body>
21     <form action="" method="POST">
22         <input type="hidden" name="_method" value="DELETE">
23     form>
24 
25     This Is All Employee
26     <c:if test="${empty requestScope.employee }">
27         没有任何的员工信息
28     c:if>
29     <c:if test="${!empty requestScope.employee }">
30         <table border="1" cellpadding="10" cellspacing="0">
31             <tr>            
32                 <th>IDth>
33                 <th>LastNameth>
34                 <th>Emailth>
35                 <th>Genderth>
36                 <th>Departmentth>
37                 <th>Editth>
38                 <th>Deleteth>
39             tr>
40             <c:forEach items="${requestScope.employee}" var="emp">
41                 <tr>
42                     <td>${emp.id}td>
43                     <td>${emp.lastName}td>
44                     <td>${emp.email}td>
45                     <td>${emp.gender==0?'Female':'Male'}td>
46                     <td>${emp.department.departmentName}td>
47                     <td><a href="emp/${emp.id}">Edita>td>
48                     <td><a href="emp/${emp.id}" class="delete">Deletea>td>
49                 tr>
50             c:forEach>
51         table>
52     c:if>
53     <br><br>
54     <a href="emp">Add New Employeea>
55 body>
56 html>

 

2.java

  主要有三个部分:

  弹出到修改的页面函数

  更新的函数

  保证lastname不改变的函数。

 1 package com.spring.it.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.ModelAttribute;
 8 import org.springframework.web.bind.annotation.PathVariable;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.bind.annotation.ResponseBody;
13 import org.springframework.web.servlet.view.RedirectView;
14 
15 import com.spring.it.dao.DepartmentDao;
16 import com.spring.it.dao.EmployeeDao;
17 import com.spring.it.enties.Department;
18 import com.spring.it.enties.Employee;
19 
20 @Controller
21 public class EmployeeHander {
22     @Autowired(required=true)
23     private EmployeeDao employeeDao;
24     
25     @Autowired(required=true)
26     private DepartmentDao departmentDao;
27 
28     /**
29      * 使得lastName不被修改,使用ModelAttribute
30      */
31     @ModelAttribute
32     public void getEmployee(@RequestParam(value="id",required=false) Integer id,Map map) {
33         if(id!=null) {
34             map.put("employee", employeeDao.get(id));
35         }
36     }
37     
38     /**
39      * 编辑,主要是提交
40      */
41     @RequestMapping(value="/emp",method=RequestMethod.PUT)
42     public String update(Employee employee) {
43         employeeDao.save(employee);
44         return "redirect:/emps";
45     }
46         
47     /**
48      * 编辑,主要是跳转到要编辑的页面
49      */
50     @RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
51     public String input(@PathVariable("id") Integer id,Map map) {
52         map.put("department", departmentDao.getDepartments());
53         //回显
54         map.put("employee", employeeDao.get(id));
55         return "input";
56     }
57     
58     /**
59      * 删除操作
60      */
61     @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
62     @ResponseBody
63     public String delete(@PathVariable("id") Integer id) {
64         employeeDao.delete(id);
65         return "redirect:/emps";
66     }
67     
68     /**
69      * 保存,是submit提交过来的请求,属于Post请求
70      */
71     @RequestMapping(value="/emp",method=RequestMethod.POST)
72     public String save(Employee employee) {
73         employeeDao.save(employee);
74         return "redirect:/emps";
75     }
76     
77     /**
78      * 跳转到input页面,用于添加员工,是Get请求
79      */
80     @RequestMapping(value="/emp",method=RequestMethod.GET)
81     public String input(Map map) {
82         map.put("department", departmentDao.getDepartments());
83         //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值
84         map.put("employee", new Employee());
85         return "input";
86     }
87     
88     /**
89      * 展示所有的员工
90      */
91     @RequestMapping("/emps")
92     public String list(Map map) {
93         System.out.println("====");
94         map.put("employee", employeeDao.getAll());
95         return "list";
96     }
97 }

 

3.input页面

  根据id是否存在,决定lastname是否显示。

  id决定是否进行走PUT。

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
 4 <%@ page import="java.util.Map"%>
 5 <%@ page import="java.util.HashMap"%>
 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 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 heretitle>
12 head>
13 <body>
14     
15     
16     <form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee">
17         
18         <c:if test="${employee.id == null}">
19             LastName:<form:input path="lastName"/><br>
20         c:if>
21         <c:if test="${employee.id != null}">
22             <form:hidden path="id"/>
23             
24             <input type="hidden" name="_method" value="PUT"/>
25         c:if>
26         
27         Email:<form:input path="email"/><br>
28         <%
29             Map<String,String> genders=new HashMap();
30             genders.put("1", "Male");
31             genders.put("0", "Female");
32             request.setAttribute("genders", genders);
33         %>
34         Gender:<form:radiobuttons path="gender" items="${genders}"/><br>
35         Department:<form:select path="department.id" 
36                         items="${department}" itemLabel="departmentName" itemValue="id">form:select><br>
37         <input type="submit" values="Submit">
38     form:form>
39 body>
40 html>

 

4.效果

  SpringMVC中CRUD实例_第8张图片

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二:

你可能感兴趣的:(SpringMVC中CRUD实例)