简易的员工信息管理系统(四)

简易的员工信息管理系统—— 第四部分:通知添加、查看、删除

员工信息管理系统—— 第一部分:登录与注册
员工信息管理系统—— 第二部分:信息添加、修改、查看
员工信息管理系统—— 第三部分:工资添加、查询

完整项目地址

用到的JavaBean:

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
public class Notice {
private int notice_id;
private String notice_title;
private String notice_content;
private String notice_date;

public int getNotice_id() {
return notice_id;
}

public void setNotice_id(int notice_id) {
this.notice_id = notice_id;
}

public String getNotice_title() {
return notice_title;
}

public void setNotice_title(String notice_title) {
this.notice_title = notice_title;
}

public String getNotice_content() {
return notice_content;
}

public void setNotice_content(String notice_content) {
this.notice_content = notice_content;
}

public String getNotice_date() {
return notice_date;
}

public void setNotice_date(String notice_date) {
this.notice_date = notice_date;
}

public Notice() {
super();
// TODO Auto-generated constructor stub
}

public Notice(String notice_title, String notice_content, String notice_date) {
super();
this.notice_title = notice_title;
this.notice_content = notice_content;
this.notice_date = notice_date;
}

@Override
public String toString() {
return "Notice [notice_id=" + notice_id + ", notice_title=" + notice_title + ", notice_content="
+ notice_content + ", notice_date=" + notice_date + "]";
}
}

XML文档:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<Notices>
<Notice notice_id="1">
<notice_tittle>2017</notice_tittle>
<notice_content>年终奖</notice_content>
<notice_date>2017-12-07 20:22:00</notice_date>
</Notice>
<Notice notice_id="3">
<notice_tittle>2017年11月份工资</notice_tittle>
<notice_content>2017年11月份工资已经发放,请注意查收。</notice_content>
<notice_date>2017-12-11 19:01:20</notice_date>
</Notice>
<Notice notice_id="5">
<notice_tittle>加班通知</notice_tittle>
<notice_content>2017年12月16.17照常上班。</notice_content>
<notice_date>2017-12-13 16:43:30</notice_date>
</Notice>
</Notices>

Java代码:

添加通知:

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
public static boolean AddNotice(Notice no) throws FileNotFoundException, JDOMException, IOException {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new FileInputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
Element root = doc.getRootElement();
Element Notice = new Element("Notice");

Attribute notice_id = new Attribute("notice_id", root.getContentSize() + "");
Element notice_tittle = new Element("notice_tittle");
Element notice_content = new Element("notice_content");
Element notice_date = new Element("notice_date");

notice_tittle.addContent(no.getNotice_title());
notice_content.addContent(no.getNotice_content());
notice_date.addContent(no.getNotice_date());

Notice.setAttribute(notice_id);
Notice.addContent(notice_tittle);
Notice.addContent(notice_content);
Notice.addContent(notice_date);

root.addContent(Notice);

Format f = Format.getPrettyFormat();
f.setEncoding("utf-8");
XMLOutputter xmlOut = new XMLOutputter(f);
xmlOut.output(doc, new FileOutputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
System.out.println("添加通知成功");
return true;
}

更新通知

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
public boolean update(Notice no, int notice_id) {
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> ch = root.getChildren();
for (Element f : ch) {
String no_id = f.getAttributeValue("notice_id");
if (no_id.equals(notice_id + "")) {
System.out.println("正在更新第" + notice_id + "个通知");

Element notice_tittle = f.getChild("notice_tittle");
notice_tittle.setText(no.getNotice_title());

Element notice_content = f.getChild("notice_content");
notice_content.setText(no.getNotice_content());

Element notice_date = f.getChild("notice_date");
notice_date.setText(no.getNotice_date());

Format f1 = Format.getPrettyFormat();
f1.setEncoding("utf-8");
XMLOutputter xmlOut = new XMLOutputter(f1);
xmlOut.output(doc, new FileOutputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
System.out.println("更新完成");
return true;
}
}
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

查询所有通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static List<Notice> getAll() {
List<Notice> list = new ArrayList<Notice>();
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> ch = root.getChildren();
for (Element element : ch) {
Notice no = new Notice();
no.setNotice_id(Integer.valueOf(element.getAttributeValue("notice_id")));
no.setNotice_title(element.getChildText("notice_tittle"));
no.setNotice_content(element.getChildText("notice_content"));
no.setNotice_date(element.getChildText("notice_date"));
list.add(no);
}
return list;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

查看通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static Notice getbyID(int notice_id) {
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> ch = root.getChildren();
for (Element element : ch) {
if (Integer.valueOf(element.getAttributeValue("notice_id")) == notice_id) {
Notice no = new Notice();
no.setNotice_id(Integer.valueOf(element.getAttributeValue("notice_id")));
no.setNotice_title(element.getChildText("notice_tittle"));
no.setNotice_content(element.getChildText("notice_content"));
no.setNotice_date(element.getChildText("notice_date"));
return no;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

删除通知

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
public static boolean delete(int notice_id) {
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> ch = root.getChildren();
for (Element f : ch) {
String id = f.getAttributeValue("notice_id");
if (id == null)
continue;
if (id.equals(notice_id + "")) {
System.out.println("正在删除通知");
f.removeAttribute("notice_id");
f.removeChild("notice_tittle");
f.removeChild("notice_content");
f.removeChild("notice_date");

Format f1 = Format.getPrettyFormat();
f1.setEncoding("utf-8");
XMLOutputter xmlOut = new XMLOutputter(f1);
xmlOut.output(doc, new FileOutputStream("E:/Code/XML/xml/src/com/notice/Notice.xml"));
System.out.println("删除通知成功");
return true;
}
}
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

JSP页面:

Notice.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
<%@page import="com.notice.Notice"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.*"%>
<!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=utf-8">
<title>查看所有通知</title>
<link rel=stylesheet href="css/bootstrap/css/bootstrap.css">
<link href="css/base.css" type="text/css" rel="stylesheet" />
<link href="css/resume.css" type="text/css" rel="stylesheet" />
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="/scripts/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<%
int id = (Integer)request.getSession().getAttribute("id");
if(id == 1){
%>
<jsp:include page="AdminTop.jsp"></jsp:include>
<%
}else{
%>
<jsp:include page="top.jsp"></jsp:include>
<%
}
int num = 0;
%>
<div class="row login-box">
<div class="col-md-10 col-md-offset-1">
<div class='panel panel-primary'>
<div class="panel-heading">查看通知</div>
<table class="table table-hover table-bordered"
contenteditable="true">
<thead>
<tr>
<th width="5%">序号</th>
<th width="20%">发布时间</th>
<th width="55%">标题</th>
<th width="20%">操作</th>
</tr>
</thead>
<tbody>
<%
List<Notice> list = new Notice().getAll();
for(Notice no : list){
%>
<tr>
<td><%=++num %></td>
<td><%=no.getNotice_date() %></td>
<td><%=no.getNotice_title() %></td>
<td><a
href="NoticeServlet?type=view&&notice_id=<%=no.getNotice_id() %>"
class="btn btn-info btn-xs" role="button">查看通知</a> <%
if(id == 1){
%> <a
href="NoticeServlet?type=delect&&notice_id=<%=no.getNotice_id() %>"
class="btn btn-danger btn-xs" role="button">删除通知</a> <%}
} %></td>
</tr>

</tbody>
</table>

</div>
<br>
</div>
</div>

</body>
</html>

这里写图片描述

AddNotice.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
<%@ 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=utf-8">
<title>发布通知</title>
<link rel=stylesheet href="css/bootstrap/css/bootstrap.css">
<link href="css/base.css" type="text/css" rel="stylesheet" />
<link href="css/resume.css" type="text/css" rel="stylesheet" />
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="/scripts/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<%
int id = (Integer)request.getSession().getAttribute("id");
if(id == 1){
%>
<jsp:include page="AdminTop.jsp"></jsp:include>
<%
}else{
%>
<jsp:include page="top.jsp"></jsp:include>
<%
}
%>
<div class="col-md-8 col-md-offset-2">

<div class="panel panel-default">
<!-- 标题 -->
<div class="panel-heading">
<h3 class="panel-title">发布通知</h3>
</div>

<div class="panel-body">

<div class="panel-body">
<form role="form" method="post" action="NoticeServlet?type=add">
<div class="form-group">
<label for="name">通知标题</label>

<textarea class="form-control" rows="2" name="notice_title"
id="notice_title"></textarea>
<br> <label for="name">通知内容</label>
<textarea class="form-control" rows="7" name="notice_content"
id="notice_content"></textarea>

</div>
<div align="center">
<button type="submit" class="btn btn-primary">发布</button>

<button type="reset" class="btn btn-warning">清空</button>
</div>
</form>
</div>
</div>
</div>
</div>



</body>
</html>

这里写图片描述

Servlet:

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
package com.notice;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.JDOMException;

/**
* Servlet implementation class NoticeServlet
*/
@WebServlet("/NoticeServlet")
public class NoticeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public NoticeServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

String type = request.getParameter("type");
if (type.equals("add")) {
Notice no = this.requestDataObj(request);
try {
new Notice();
Notice.AddNotice(no);

} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

request.getRequestDispatcher("Notice.jsp").forward(request, response);
}
if (type.equals("select")) {

request.getRequestDispatcher("Notice.jsp").forward(request, response);
}
/*
* if(type.equals("update")){ int notice_id =
* Integer.valueOf(request.getParameter("notice_id")); Notice no =
* this.requestDataObj(request); new Notice().update(no, notice_id);
* boolean a = new Notice().update(no, notice_id);
*
* Notice notice = new Notice().getbyID(notice_id);
* request.getSession().setAttribute("no_list", notice);
* request.getRequestDispatcher("ViewNotice.jsp").forward(request,
* response);
*
* }
*/
if (type.equals("delect")) {
int notice_id = Integer.valueOf(request.getParameter("notice_id"));
new Notice();
Notice.delete(notice_id);

request.getRequestDispatcher("Notice.jsp").forward(request, response);
}
if (type.equals("view")) {
int notice_id = Integer.valueOf(request.getParameter("notice_id"));
new Notice();
Notice no = Notice.getbyID(notice_id);
request.getSession().setAttribute("no_list", no);
request.getRequestDispatcher("ViewNotice.jsp").forward(request, response);
}

}

private Notice requestDataObj(HttpServletRequest request) {
Notice no = new Notice();
String title = request.getParameter("notice_title");
String content = request.getParameter("notice_content");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
no = new Notice(title, content, date);
return no;
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
---------------- The End ----------------
May丶乘剑 wechat
欢迎扫描二维码关注我的微信公众号~~
0%