好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring MVC实现一次简单的CRUD示例

基本环境搭建

1、数据库 和 实体类 的名字相同,实体类 属性名即 数据库 字段名。

2、创建 实体类 对应 dao 类,持久层框架 mybatis 正处于学习中,这里就用原始的 jdbc 操作了。

3、创建一个 java 类,作为 controller,处理请求。

4、crud 涉及到 显示数据、修改、添加的页面;删除就不需要了,修改和添加使用同一个页面。所以就有 index.jsp(一个超链接跳转到 show.jsp)、show.jsp(显示所有员工信息和操作链接)、input.jsp(用于修改和添加)。

具体实现

接下来就是 crud 的具体实现了,顺序为 查询显示所有员工信息-->删除-->添加-->修改。

在具体的实现前,需要了解一下 controller 类的大致结构:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.taohan.controller;

 

//import *;

 

@controller

@suppresswarnings ( "all" )

public class curd {

 

  @autowired

  private departmentinfodao departdao;

  @autowired

  private employeeinfodao empdao;

 

   //在后面的具体描述中,就只贴出 处理对应请求的方法代码了

}

查询

① 在 index.jsp 页面中加入下面超链接,用于 获取到所有员工集合,保存到 域对象 中,最后 转发(内部跳转) 到 show.jsp 进行显示。

?

1

<a href= "emps" rel= "external nofollow" >员工信息</a>

② 该请求会交由 controller 中的指定方法处理,下面就需要定义处理该请求的方法。

?

1

2

3

4

5

6

7

8

//获取所有的员工信息

  @requestmapping ( "/emps" )

  public string getemps(map<string, object> map) {

   //获取员工集合,存入 map 集合中

   map.put( "emps" , empdao.getemployees());

  

   return "show" ;

  }

③ 现在已经获取到了员工集合了,接下来就是编写 show.jsp 页面了。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %>

<%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %>

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ;

%>

 

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >

<html>

  <head>

   <base href= "<%=basepath%>" >

  

   <title>员工信息</title>

  

   <meta http-equiv= "pragma" content= "no-cache" >

   <meta http-equiv= "cache-control" content= "no-cache" >

   <meta http-equiv= "expires" content= "0" > 

   <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >

   <meta http-equiv= "description" content= "this is my page" >

   <!--

     这里导入了一个 jquery 文件,属于静态资源了

     静态资源不能被加载的处理:

     在 springmvc 的配置文件中加入

     <mvc:annotation-driven></mvc:annotation-driven>

     <mvc: default -servlet-handler/>

    详细信息后面我也会具体说明

   -->

   <script type= "text/javascript" src= "${pagecontext.request.contextpath }/js/jquery.js" ></script>

  </head>

 

  <body>

   <table border= "1" cellpadding= "3" cellspacing= "0" >

     <caption>员工信息</caption>

     <tr>

       <th>编号</th>

       <th>姓名</th>

       <th>性别</th>

       <th>年龄</th>

       <th>地址</th>

       <th>部门</th>

       <th>操作</th>

     </tr>

     <c:choose>

       <c:when test= "${not empty requestscope.emps }" >

         <c:foreach items= "${requestscope.emps }" var= "emp" >

           <tr>

             <td>${emp.employeeid }</td>

             <td>${emp.employeename }</td>

             <td>

               <c: if test= "${emp.employeesex == 1 }" >男</c: if >

               <c: if test= "${emp.employeesex == 0 }" >女</c: if >

             </td>

             <td>${emp.employeeage }</td>

             <td>${emp.employeeaddr }</td>

             <td>${emp.depart.departname }</td>

             <td>

               <!--

                 这里可以有两种方式进行删除,值得注意的是:这两种删除的方法对应的 url 是有区别的。

                 但是,本文主要就是使用 rest 风格的 url 进行操作,所以,还是使用第二种方法吧。

                -->         

               <!-- <a href= "${pagecontext.request.contextpath }/del/${emp.employeeid }" >删除</a> -->

               <a onclick= "return del(this)" href= "${pagecontext.request.contextpath }/emp/${emp.employeeid }" >删除</a>

               <a href= "update/${emp.employeeid }" >修改</a>

             </td>

           </tr>

         </c:foreach>

       </c:when>

     </c:choose>

   </table>

   <form action= "emp" method= "post" >

     <!-- 用于将 post 请求转换为 delete请求 -->

     <input type= "hidden" value= "delete" name= "_method" />

   </form>

   <script type= "text/javascript" >

     //将 get 请求转换为 post 请求提交

     function del(tag) {

       //获取当前请求路径

       var href = tag.href;

       //提交

       $( "form" ).attr( "action" , href).submit();

       return false ;

     }

   </script>

   <a href= "preadd" >添加员工</a>

  </body>

</html>

到这里,整个查询就结束了!

删除

在上面的 show.jsp 页面代码中有两种请求方式可以进行删除。

㈠ 使用 get 请求进行删除

使用此请求方式的请求路径大致为:127.0.0.1:8080/项目名/del/id。那么,后台 controller 类中就要有一个对应处理 /del/id 这样请求的方法。

?

1

2

3

4

5

6

7

8

9

10

11

//根据员工编号 删除员工

   @requestmapping (value= "/del/{id}" )

   public string delemp( @pathvariable ( "id" ) integer id) {

     // row 受影响行数,这里就不做成功与否的判断了

     int row = empdao.isdel(id);

    

     //这里的请求路径为 del/id 是二级目录

     //redirect:前缀表示重定向到 emps 路径,就是前面查询的路径,默认(不写)是转发。

     //删除后要重新获取员工数据,再转发到 show.jsp 显示,不能直接转发到 show.jsp 页面,因为并没有数据,所以需要先查询,再转发。

     return "redirect:emps" ;

   }

上面就是使用普通 get 请求处理删除请求。

㈡ 使用 delete 请求进行删除

我们知道,hiddenhttpmethodfilter 类可以将 post 请求转换为 我们指定的其他请求方式。在我的这篇文章主要讲解了 hiddenhttpmethodfilter 类的处理过程。这里,由于在 show.jsp 页面中的删除超链接是一个 get 请求。所以,我们需要使用 js 将 get 请求使用 post 请求发送,并且转换为 delete 请求。所以,在 show.jsp 页面就有了一个表单 和 一个隐藏的 input 标签。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<form action= "emp" method= "post" >

     <!-- 用于将 post 请求转换为 delete请求 -->

     <input type= "hidden" value= "delete" name= "_method" />

   </form>

   <script type= "text/javascript" >

     //将 get 请求转换为 post 请求提交

     function del(tag) {

       //获取当前请求路径

       var href = tag.href;

       //提交

       $( "form" ).attr( "action" , href).submit();

       return false ;

     }

   </script>

使用此请求方式的请求路径大致为:127.0.0.1:8080/项目名/emp/id。那么,后台 controller 类中就要有一个对应处理 url 为:/emp/id,请求方式为:delete  这样的请求的方法。

?

1

2

3

4

5

6

7

//根据员工编号 删除员工 使用 delete 请求,method属性就指定了请求方式

   @requestmapping (value= "/emp/{id}" , method=requestmethod.delete)

   public string delemp( @pathvariable ( "id" ) integer id) {

     int row = empdao.isdel(id);

    

     return "redirect:emps" ;

   }

上面代码中出现了一个 requestmethod,它是一个 枚举类型,其中的值就是各种请求方式。

(requestmethod)

以上就是两种使用两种方式进行删除的示例了!

添加

① 在 show.jsp 页面最下面有下面这样的一个超链接

?

1

<a href= "preadd" >添加员工</a>

该链接用于跳转到 input.jsp 页面,至于需要后台进行处理,是因为需要加载出所有的部门,用于添加的时候选择。

② 处理 preadd 请求,获取部门列表

?

1

2

3

4

5

6

7

8

9

10

//加载添加页面

   @requestmapping ( "/preadd" )

   public string preadd(map<string, object> map) {

     //获取部门集合

     map.put( "departments" , departdao.getdeparts());

     //input.jsp 页面需要一个 员工对象,但是添加没有,所以给个空的,至于为什么需要,见文末

     map.put( "employee" , new employeeinfo());

    

     return "input" ;

   }

③ input.jsp 页面,添加和修改公用该页面

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %>

<%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ;

%>

 

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >

<html>

  <head>

   <base href= "<%=basepath%>" >

   <title>添加/修改页面示例</title>

   <meta http-equiv= "pragma" content= "no-cache" >

   <meta http-equiv= "cache-control" content= "no-cache" >

   <meta http-equiv= "expires" content= "0" > 

   <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >

   <meta http-equiv= "description" content= "this is my page" >

  </head>

 

  <body>

   <form:form action= "emp" method= "post" modelattribute= "employee" >

     <%-- employee对象有编号就是修改 --%>

     <c: if test= "${requestscope.employee.employeeid != null }" >

       编号:<form:input path= "employeeid" readonly= "true" /><br /><br />

       <input type= "hidden" value= "put" name= "_method" />

     </c: if >

     姓名:<form:input path= "employeename" />

     <br /><br />

     性别:<form:radiobutton path= "employeesex" value= "1" label= "男" />

        <form:radiobutton path= "employeesex" value= "0" label= "女" />

     <br /><br />

     年龄:<form:input path= "employeeage" />

     <br /><br />

     地址:<form:input path= "employeeaddr" />

     <br /><br />

     部门:<form:select path= "depart.departid" items= "${requestscope.departments }"

      itemlabel= "departname" itemvalue= "departid" ></form:select>

     <br /><br />

     <input type= "submit" value= "提交" />

   </form:form>

  </body>

</html>

 

input.jsp 页面代码

从 input.jsp 页面代码中可以看出,添加和修改使用了同一个提交路径(/emp 添加和修改后台需要的就只是一个对象而已),但是它们的请求方式是不同的。

添加操作并没有做任何处理,就是一个 post 请求。

④ 添加员工

?

1

2

3

4

5

6

7

//添加 post 请求就是添加

   @requestmapping (value= "/emp" , method=requestmethod.post)

   public string isadd(employeeinfo emp) {

     int row = empdao.isadd(emp);

    

     return "redirect:/emps" ;

   }

小结: 添加 和 修改使用同一页面,那么就需要有标识判断 进入当前页面(input.jsp) 的是什么操作,我们就可以根据 员工编号 作为判断标识,如果该员工没有编号(在 /preadd 请求处理方法中创建了一个空的员工对象放到了域对象中)就是添加;如果有员工编号就是修改,则使用 _method 设置需要将 post 请求转换为 put 请求。

修改

① 在 show.jsp 页面中每个员工后面都有一个修改链接,如下:

?

1

<a href= "update/${emp.employeeid }" >修改</a>

② 后台代码处理 update/id 请求,获取要修改的员工对象,放入域对象,转发到 input.jsp 页面显示。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

//加载修改页面

   @requestmapping (value= "/update/{id}" , method=requestmethod.get)

   public string preupdate( @pathvariable ( "id" ) integer id,

       map<string, object> map) {

     //根据编号获取到要修改的员工对象

     employeeinfo emp = empdao.getempbyid(id);

     map.put( "employee" , emp);

    

     //加载所有部门信息

     map.put( "departments" , departdao.getdeparts());

    

     return "input" ;

   }

③ 在 input.jsp 页面的 springmvc自带的 form标签会将对应的员工属性信息显示在对应的文本框中

④ 修改操作就会有一个隐藏的 input 标签,如下:

?

1

<input type= "hidden" value= "put" name= "_method" />

这样,当修改时 post 请求便会转换为 put 请求。

⑤ controller 类中,就定义方法处理请求url为: /emp,请求方式为:put 的请求

?

1

2

3

4

5

6

7

//修改

   @requestmapping (value= "/emp" , method=requestmethod.put)

   public string isupdate(employeeinfo emp) {

     int row = empdao.isupdate(emp);

    

     return "redirect:/emps" ;

   }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:http://www.cnblogs.com/dream-saddle/p/9391739.html

查看更多关于Spring MVC实现一次简单的CRUD示例的详细内容...

  阅读:41次