Skip to content

Commit 4dc8620

Browse files
committed
新增学生、删除学生完成
1 parent faa06f7 commit 4dc8620

File tree

8 files changed

+191
-20
lines changed

8 files changed

+191
-20
lines changed

imoocSSHStudentManagementSystem/WebRoot/jsp/students/Students_add.jsp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
77
<!DOCTYPE html>
88
<html>
99
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
10-
<link rel="stylesheet" type="text/css" href="../css/default.css" />
10+
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/default.css" />
1111
<style type="text/css">
1212
* {
1313
background: none repeat scroll 0 0 transparent;
@@ -76,12 +76,12 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
7676
}
7777
</style>
7878
<body>
79-
<script type="text/javascript" src="../js/Calendar3.js"></script>
79+
<script type="text/javascript" src="${pageContext.request.contextPath}/js/Calendar3.js"></script>
8080

8181
<div id="navi">
8282
<div id='naviDiv'>
83-
<span><img src="../images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;学生管理<span>&nbsp;
84-
<span><img src="../images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;<a href="<%=path%>/students/Students_query.action">学生列表</a><span>&nbsp;
83+
<span><img src="${pageContext.request.contextPath}/images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;学生管理<span>&nbsp;
84+
<span><img src="${pageContext.request.contextPath}/images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;<a href="<%=path%>/Students_query.action">学生列表</a><span>&nbsp;
8585
</div>
8686
</div>
8787
<div id="tips">
@@ -91,7 +91,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
9191
<strong>添加学生资料</strong>
9292
<br>
9393
<br>
94-
<form name="addForm" action="<%=path%>/students/Students_add.action" method="post">
94+
<form name="addForm" action="<%=path%>/Students_add.action" method="post">
9595
<table width="400" >
9696
<tr>
9797
<td width="30%">姓名:</td>

imoocSSHStudentManagementSystem/WebRoot/jsp/students/Students_add_success.jsp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
66
<!DOCTYPE html>
77
<html>
88
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
9-
<link rel="stylesheet" type="text/css" href="../css/default.css" />
9+
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/default.css" />
1010
<style type="text/css">
1111
* {
1212
background: none repeat scroll 0 0 transparent;
@@ -79,15 +79,15 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
7979
<div id="navi">
8080
<!-- 导航空白 -->
8181
<div id='naviDiv'>
82-
<span><img src="../images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;学生管理<span>&nbsp;
83-
<span><img src="../images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;<a href="<%=path%>/students/Students_query.action">学生列表</a><span>&nbsp;
82+
<span><img src="${pageContext.request.contextPath}/images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;学生管理<span>&nbsp;
83+
<span><img src="${pageContext.request.contextPath}/images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;<a href="<%=path%>/Students_query.action">学生列表</a><span>&nbsp;
8484
</div>
8585
</div>
8686
<div id="tips">
8787
<!-- 导航空白 -->
8888
</div>
8989
<div id="mainContainer">
90-
<strong>添加成功,<a href="<%=path%>/students/Students_add.jsp">继续添加?</a></strong>
90+
<strong>添加成功,<a href="<%=path%>/jsp/students/Students_add.jsp">继续添加?</a></strong>
9191
<!--数据表格空白 -->
9292
</div>
9393
</body>

imoocSSHStudentManagementSystem/WebRoot/jsp/students/Students_modify.jsp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
77
<!DOCTYPE html>
88
<html>
99
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
10-
<link rel="stylesheet" type="text/css" href="../css/default.css" />
10+
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/default.css" />
1111
<style type="text/css">
1212
* {
1313
background: none repeat scroll 0 0 transparent;
@@ -80,8 +80,8 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
8080

8181
<div id="navi">
8282
<div id='naviDiv'>
83-
<span><img src="../images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;学生管理<span>&nbsp;
84-
<span><img src="../images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;<a href="<%=path%>/students/Students_query.action">学生列表</a><span>&nbsp;
83+
<span><img src="${pageContext.request.contextPath}/images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;学生管理<span>&nbsp;
84+
<span><img src="${pageContext.request.contextPath}/images/arror.gif" width="7" height="11" border="0" alt=""></span>&nbsp;<a href="<%=path%>/Students_query.action">学生列表</a><span>&nbsp;
8585
</div>
8686
</div>
8787
<div id="tips">
@@ -92,7 +92,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
9292
<br>
9393
<br>
9494

95-
<form name="modifyForm" action="<%=path%>/students/Students_save.action" method="post">
95+
<form name="modifyForm" action="<%=path%>/Students_save.action" method="post">
9696
<table width="400" >
9797
<tr>
9898
<td width="30%">学号:</td>

imoocSSHStudentManagementSystem/WebRoot/jsp/students/Students_query_success.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
116116
<s:iterator value="#session.students_list" var="stu">
117117
<tr class="list">
118118
<td><s:property value="#stu.sid"/></td>
119-
<td><a href="<%=path%>/students/Students_modify.action?sid=<s:property value="#stu.sid"/>"><s:property value="#stu.sname"/></a></td>
119+
<td><a href="<%=path%>/Students_modify.action?sid=<s:property value="#stu.sid"/>"><s:property value="#stu.sname"/></a></td>
120120
<td><s:property value="#stu.gender"/></td>
121121
<td><s:date name="#stu.birthday" format="yyyy年MM月dd日"/></td>
122122
<td><s:property value="#stu.address"/></td>
123-
<td><a href="<%=path%>/students/Students_delete.action?sid=<s:property value="#stu.sid"/>" onclick="javascript: return confirm('真的要删除吗?');">删除</a></td>
123+
<td><a href="<%=path%>/Students_delete.action?sid=<s:property value="#stu.sid"/>" onclick="javascript: return confirm('真的要删除吗?');">删除</a></td>
124124
</tr>
125125
</s:iterator>
126126
<!-- 遍历结束 -->

imoocSSHStudentManagementSystem/src/com/southeast/iLoveMasami/action/StudentsAction.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.southeast.iLoveMasami.action;
22

3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
36
import java.util.List;
47

58
import com.southeast.iLoveMasami.entity.Students;
@@ -27,4 +30,45 @@ public String query()
2730
}
2831
return "query_success";
2932
}
33+
//删除学生动作
34+
public String delete()
35+
{
36+
StudentsDao sdao = new StudentsDaoImpl();
37+
//获得传过来的参数
38+
String sid=request.getParameter("sid");
39+
sdao.deleteStudents(sid);
40+
return "delete_success";
41+
}
42+
//添加学生动作
43+
public String add()
44+
{
45+
StudentsDao sdao = new StudentsDaoImpl();
46+
Students s = new Students();
47+
s.setSname(request.getParameter("sname"));
48+
s.setGender(request.getParameter("gender"));
49+
String dateString = request.getParameter("birthday");
50+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
51+
Date date;
52+
try {
53+
date = sdf.parse(dateString);
54+
s.setBirthday(date);
55+
} catch (ParseException e) {
56+
e.printStackTrace();
57+
}
58+
s.setAddress(request.getParameter("address"));
59+
sdao.addStudents(s);
60+
return "add_success";
61+
}
62+
63+
//修改学生动作
64+
public String modify()
65+
{
66+
//获得学生编号
67+
String sid=request.getParameter("sid");
68+
StudentsDao sdao=new StudentsDaoImpl();
69+
Students s = sdao.queryStudentsBySid(sid);
70+
//保存到会话中
71+
session.setAttribute("modify_students", s);
72+
return "modify_success";
73+
}
3074
}

imoocSSHStudentManagementSystem/src/com/southeast/iLoveMasami/serviceimpl/StudentsDaoImpl.java

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,47 @@ public List<Students> queryAllStudents() {
4646
@Override
4747
public Students queryStudentsBySid(String sid) {
4848

49-
return null;
49+
Transaction tx=null;
50+
Students s = null;
51+
try{
52+
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
53+
session.beginTransaction();
54+
s = (Students) session.get(Students.class, sid);
55+
56+
return s;
57+
}
58+
catch(Exception ex){
59+
ex.printStackTrace();
60+
61+
return s;
62+
}
63+
finally{
64+
if(tx!=null)
65+
{
66+
tx = null;
67+
}
68+
}
5069
}
5170

5271
@Override
5372
public boolean addStudents(Students s) {
54-
55-
return false;
73+
Transaction tx=null;
74+
s.setSid(getNewSid());//设置学生的学号
75+
try{
76+
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
77+
tx=session.beginTransaction();
78+
session.save(s);
79+
tx.commit();
80+
return true;
81+
}
82+
catch(Exception ex){
83+
ex.printStackTrace();
84+
return false;
85+
}
86+
finally{
87+
if(tx!=null)
88+
tx=null;
89+
}
5690
}
5791

5892
@Override
@@ -63,8 +97,68 @@ public boolean updateStudents(Students s) {
6397

6498
@Override
6599
public boolean deleteStudents(String sid) {
66-
67-
return false;
100+
Transaction tx=null;
101+
try{
102+
//获得会话对象
103+
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
104+
tx=session.beginTransaction();
105+
Students s=(Students) session.get(Students.class, sid);
106+
session.delete(s);
107+
tx.commit();
108+
return true;
109+
}
110+
catch(Exception ex){
111+
ex.printStackTrace();
112+
tx.commit();
113+
return false;
114+
}
115+
finally{
116+
if(tx!=null)
117+
tx=null;
118+
}
119+
}
120+
121+
private String getNewSid()
122+
{
123+
Transaction tx=null;
124+
String hql="";
125+
String sid=null;
126+
try{
127+
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
128+
tx=session.beginTransaction();
129+
//获得当前学生的最大编号
130+
hql = "select max(id) from Students";
131+
Query query = session.createQuery(hql);
132+
sid = (String) query.uniqueResult();
133+
if(sid==null||sid.isEmpty())
134+
{
135+
//给一个默认的最大编号
136+
sid = "S001";
137+
}
138+
else{
139+
int oldNum = Integer.parseInt(sid.substring(1));
140+
oldNum++;
141+
String newNum = String.valueOf(oldNum);
142+
int numLen = newNum.length();
143+
for(int i=0;i<3-numLen;++i)
144+
{
145+
newNum='0'+newNum;
146+
}
147+
sid = 'S' + newNum;
148+
}
149+
tx.commit();
150+
return sid;
151+
}
152+
catch(Exception ex)
153+
{
154+
ex.printStackTrace();
155+
tx.commit();
156+
return null;
157+
}
158+
finally{
159+
if(tx!=null)
160+
tx=null;
161+
}
68162
}
69163

70164
}

imoocSSHStudentManagementSystem/src/struts.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<package name="students" namespace="/" extends="default">
1818
<action name="*_*" class="com.southeast.iLoveMasami.action.{1}Action" method="{2}">
1919
<result name="query_success">/jsp/students/Students_query_success.jsp</result>
20+
<result name="delete_success" type="chain">Students_query</result>
21+
<result name="add_success">/jsp/students/Students_add_success.jsp</result>
22+
<result name="modify_success">/jsp/students/Students_modify.jsp</result>
2023
<result name="input">/jsp/students/Students_query_success.jsp</result>
2124

2225
</action>

imoocSSHStudentManagementSystem/test/com/southeast/iLoveMasami/servicetest/TestStudentsDaoImpl.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.southeast.iLoveMasami.servicetest;
22

3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
36
import java.util.List;
47

58
import org.junit.Test;
@@ -8,6 +11,8 @@
811
import com.southeast.iLoveMasami.service.StudentsDao;
912
import com.southeast.iLoveMasami.serviceimpl.StudentsDaoImpl;
1013

14+
import junit.framework.Assert;
15+
1116
public class TestStudentsDaoImpl {
1217
@Test
1318
public void testqueryAllStudents(){
@@ -17,5 +22,30 @@ public void testqueryAllStudents(){
1722
System.out.println(students);
1823
}
1924
}
25+
@Test
26+
public void testGetNewSid()
27+
{
28+
StudentsDaoImpl sDao = new StudentsDaoImpl();
29+
//System.out.println(sDao.getNewSid());
30+
}
31+
@Test
32+
public void testAddStudents()
33+
{
34+
Students s = new Students();
35+
s.setSname("³¤ÔóÑÅÃÀ");
36+
s.setGender("Å®");
37+
String dateString = "1987-06-03";
38+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
39+
Date date;
40+
try {
41+
date = sdf.parse(dateString);
42+
s.setBirthday(date);
43+
} catch (ParseException e) {
44+
e.printStackTrace();
45+
}
46+
s.setAddress("¾²¸ÔÏØ");
47+
StudentsDao sDao = new StudentsDaoImpl();
48+
Assert.assertEquals(true, sDao.addStudents(s));
49+
}
2050

2151
}

0 commit comments

Comments
 (0)