简单的页面分析
在上一个文章简单的数据库连接测试,已经测试和数据库做简单的交互,也就是dao层的实现,接下来要说的却是action的简单实现,在ssh中有struts作为表示层和server的交换,而这里我不是说的是关于struts这里只是简单的运用jsp的代码书写来实现数据的传输,这也是最繁琐的步骤,但是这却让我们对底层的调用有一个简单的了解,这里是直接调用封装好的数据,交换和使用,首先要书写的是action的使用,用的最多的就是getparameter表单的提交了,这里在网络上提交一个表单吗,然后通过getparameter进行获取,然后通过enployeedao中的方法进行增删改查,就能够进行基本的逻辑操作了。
代码实现
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
|
<% @page import = "dao.employeedao" %> <% @page import = "java.text.simpledateformat" %> <% @page import = "entity.employee" %> <%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!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=iso-8859-1" > <title>insert title here</title> </head> <body> <% //1、接收表单提交的参数 string action = request.getparameter( "action" ); //3.调用employeedao中addemployee(employee employee)完成员工添加 employeedao employeedao = new employeedao(); if (action.equals( "0" ) || action.equals( "1" )) { //添加 更新 string empno = request.getparameter( "empno" ); string ename = request.getparameter( "ename" ); string sal = request.getparameter( "sal" ); string hiredate = request.getparameter( "hiredate" ); //2.将数据封装至employee对象中 employee employee = new employee(); employee.setempno(integer.parseint(empno)); employee.setename(ename); employee.setsal( double .parsedouble(sal)); simpledateformat sdf = new simpledateformat( "yyyy-mm-dd" ); employee.sethiredate(sdf.parse(hiredate)); if (action.equals( "0" )) { //添加 employeedao.addemployee(employee); } else { //更新 employeedao.updateemployee(employee); } } else if (action.equals( "2" )) { //删除 string empno = request.getparameter( "empno" ); employeedao.deleteemployee(empno); } else if (action.equals( "3" )) { //批量删除 system.out.println( "action=" +action); string[] chks=request.getparametervalues( "chks" ); for (string chk:chks){ system.out.println( "chk=" +chk); } } //4.画面跳转至employeelist.jsp 重定向 response.sendredirect( "employeelist.jsp" ); %> </body> </html> |
然后我们进行简单的页面设计,运用了表格的形式进行设计,代码如下
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
<% @page import = "java.util.list" %> <% @page import = "entity.employee" %> <% @page import = "dao.employeedao" %> <% @page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!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=iso-8859-1" > <title>insert title here</title> <script> function checkall() { //1.获取chkall的选中状态checked var chkall = document.getelementbyid( "chkall" ); //alert(chkall.checked); //2、获取其他所有的checkbox var chks = document.getelementsbyname( "chks" ); for (var i = 0 ; i < chks.length; i++) { //3、将chkall的选中状态于其他checkbox的选中状态同步 chks[i].checked = chkall.checked; } } function ondelete(empno){ var result=confirm( "delete employee?" ); if (result){ window.location.href= "employeemanageaction.jsp?action=2&empno=" rel= "external nofollow" +empno; } } //function ondeletebatch(){ //判断是否有选中的checkbox //有选中的场合 //提示是否删除确认的信息 //确认删除 //提交表单 //var action=document.getelementbyid("action"); //action.value="3"; // document.frmlist.action="employeemanageaction.jsp" // document.frmlist.submit(); //取消删除 //不用处理 //无选中的场合 //提示选中记录的信息 //} </script> </head> <body> <h3>employee list page</h3> <form name= "frmsearch" action= "xxxxaction.jsp" method= "post" > ename:<input type= "text" /> <input type= "submit" name= "btnsubmit" value= "submit" /> </form> <p> <hr /> <a href= "employeemanage.jsp?action=0" rel= "external nofollow" >add employee</a> <a href= "javascript:void(0)" rel= "external nofollow" rel= "external nofollow" onclick= "ondeletebatch();" >delete employee batch</a> <p> <% employeedao emplyeedao = new employeedao(); list<employee> employees = emplyeedao.getemployees(); %> <% if (employees != null && employees.size() > 0 ) { %> <form name= "frmlist" action= "" method= "post" > <input type= "hidden" name= "action" id= "action" value= "" /> <table width= "600px" border= "1px" > <tr bgcolor= "#009966" > <td><input type= "checkbox" id= "chkall" onclick= "checkall()" /></td> <td>empno</td> <td>ename</td> <td>sal</td> <td>hiredate</td> <td>action</td> </tr> <% employee employee = null ; for ( int i = 0 ; i < employees.size(); i++) { employee = employees.get(i); %> <tr> <td><input type= "checkbox" id= "chk<%=i + 1%>" name= "chks" value= "<%=employee.getempno()%>" /></td> <td><%=employee.getempno()%></td> <td><%=employee.getename()%></td> <td><%=employee.getsal()%></td> <td><%=employee.gethiredate()%></td> <td><a href= "employeemanage.jsp?action=1&empno=<%=employee.getempno()%>" rel= "external nofollow" >update</a> <a href= "javascript:void(0)" rel= "external nofollow" rel= "external nofollow" onclick= "ondelete(<%=employee.getempno()%>);" >delete</a></td> </tr> <% } %> </table> </form> <% } %> </body> </html> |
接下来要说的是更新操作
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
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<% @page import = "entity.employee" %> <% @page import = "dao.employeedao" %> <%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!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=iso-8859-1" > <title>insert title here</title> <script> function checkform() { var empno = document.getelementbyid( "empno" ); if (empno && empno.value == "" ) { empno.focus(); alert( "please input empno" ); return ; } var ename = document.getelementbyid( "ename" ); if (ename && ename.value == "" ) { ename.focus(); alert( "please input ename" ); return ; } var sal = document.getelementbyid( "sal" ); if (sal && sal.value == "" ) { sal.focus(); alert( "please input sal" ); return ; } var hiredate = document.getelementbyid( "hiredate" ); if (hiredate && hiredate.value == "" ) { hiredate.focus(); alert( "please input hiredate" ); return ; } var frm = document.getelementbyid( "frm" ); frm.submit(); } </script> </head> <body> <% string action=request.getparameter( "action" ); employee employee= null ; if (action.equals( "1" )){ //更新操作 string empno = request.getparameter( "empno" ); employeedao employeedao = new employeedao(); employee=employeedao.getemployeebyempno(empno); } %> <h3>employee <%=action.equals( "1" )? "update" : "regist" %> page</h3> <form id= "frm" action= "employeemanageaction.jsp" method= "post" onsubmit= "return checkform();" > <input type= "hidden" name= "action" value= "<%=action%>" /> <table> <tr> <td>empno</td> <td><input type= "text" id= "empno" name= "empno" <%=action.equals( "1" )? "readonly" : "" %> value= "<%=employee==null?" ":employee.getempno()%>" /></td> </tr> <tr> <td>ename</td> <td><input type= "text" id= "ename" name= "ename" value= "<%=employee==null?" ":employee.getename()%>" /></td> </tr> <tr> <td>sal</td> <td><input type= "text" id= "sal" name= "sal" value= "<%=employee==null?" ":employee.getsal()%>" /></td> </tr> <tr> <td>hiredate</td> <td><input type= "text" id= "hiredate" name= "hiredate" value= "<%=employee==null?" ":employee.gethiredate()%>" /></td> </tr> <tr> <td><input type= "button" name= "btnsubmit" value= "submit" onclick= "checkform();" /></td> <td><input type= "reset" name= "btnreset" value= "reset" /></td> </tr> </table> </form> </body> </html> |
在这个过程中刚好用到了简单的js对数据进行非空和数据类型判断,这就是我们需要学会的逻辑,业务层,然后进行其他操作,当然我注释的是还没完成的多个删除和模糊搜索,这个下一个博客再去完善。
总结
在学习这个过程中,学会了更多关于数据调用的知识,而不是像hibernate那样在配置文件配置,然后直接调用就可以了,再用jsp中也发现jsp嵌入代码,更加笨重,这也是我学习的另一个方面吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/lxh5431/article/details/52489972