基本環境搭建
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