如有错误或需要源码欢迎留言!一键三连~笔芯
传智书城
书城首页展示:
一、开发背景
随着互联网时代的到来,社会的方方面面都在努力与现代的先进技术接轨,网络在人们的生活中应用越来越广泛。大家越来越想在最短的时间内购买到自己所需的图书。但图书种类繁多,版本不一,给购书者在繁忙的工作生活带来了很大的麻烦,于是传智书城系统应用而生。
传智书城系统的开发为书城管理者带来了很大的方便,很大限度上提高了管理工作的效率。还有效地缩短了图书流通发行的环节,将广大读者、图书、出版者、发行者紧密地结合在一起,也提高了图书流通率。
二、系统分析
(一)需求分析
传智书城系统是在现在线上购物平台四起的发展趋势下,建立的B2C网上购物系统,这种新型的图书销售形式,与传统利用书店进行销售的方式相比拥有了许多优势:传智书城系统降低销售成本、利用网络作为交易平台,改变传统的线下交易方式,使得交易活动不再受空间和时间的限制、提高工作人员的工作效率和更方便便捷的满足用户,更多存储所有数据信息及快速方便的检索功能。
(二)可行性分析
传智书城项目可行性分析包括市场可行性、技术可行性和经济可行性三个方面。
1. 市场可行性:
随着互联网的不断普及和人们获取知识的需求越来越高,传智书城项目提供了一个集图书销售、图书展示、数字阅读和用户交互于一体的综合书城平台,具有广泛的市场需求。
2. 技术可行性:
传智书城项目需要实现图书分类和搜索功能、购物车和结算功能、数字阅读和社交分享功能等,这些功能都可以通过现有的技术和框架来实现。
3. 经济可行性:传智书城项目需要投入一定的开发成本和运营成本,同时也需要进行市场推广和用户获取,但由于互联网电商行业的盈利模式比较成熟,预计可以通过销售图书、广告收入等方式实现盈利。
综合以上三个方面的分析,传智书城项目具有很好的市场、技术和经济可行性,可以进行开发和运营。
三、系统设计
(一)功能详细介绍
系统管理模块的主要功能包括:系统管理员和用户的注册、登录、退出以及用户信息修改功能,系统管理员和用户只有通过登录才能进行相关业务操作。
商品浏览模块模块的主要功能包括:首页轮播图、图书公告栏、热卖推荐、模糊搜索商品。
购物车管理模块的主要功能包括:向购物车中添加图书、修改购物车中图书数量、删除购物车中图书、我的购物车;
商品信息管理模块的主要功能包括:添加商品、修改商品、删除商品、查询商品。
公告栏管理模块的主要功能包括:添加公告、删除公告、查询公告、删除公告。
销售管理模块的主要功能包括:加入购物车、修改购物车中商品数量、删除购物车中商品、我的购物车、订单删除、下载销售数据等。
(二)系统功能结构
传智书城系统功能结构图如图1所示:
(三)系统开发环境及运行环境
系统开发环境如下:
系统开发平台:Eclipse。
数据库管理系统软件:MySQL。
Java开发包:JDK11以上。
运行环境如下:
运行平台:Windows10。
Web服务器:tomcat9.0。
(Tomcat startup.bat一闪而过端口被占用、拒绝访问、404等问题解决办法https://blog.csdn.net/weixin_51912083/article/details/131983458?spm=1001.2014.3001.5501)
浏览器:Edge浏览器。
(四)数据库设计(整体上的设计)
1.下面是根据传智书城项目的需求,为本项目的核心实体对象设计E-R图,具体如下:
四、超市管理系统的实现
(一)用户登录模块的实现
为了系统的安全考虑,登录是必不可少的一个模块。登录模块中用户和管理员的登录功能实现的方式相同,登录的验证方法类似,只是登录后所处的页面不同,用户登录后跳转到客户端页面,管理员登录后跳转到管理端页面。
1.用户登录界面如图2所示:
图2用户登录界面
图3用户登录首界面
<body class="main">
<jsp:include page="head.jsp" />
<jsp:include page="menu_search.jsp" />
<div id="divpagecontent">
<table width="100%" border="0" cellspacing="0">
<tr>
<td width="25%">
<table width="100%" border="0" cellspacing="0" style="margin-top:30px">
<tr>
<td class="listtitle">我的帐户</td>
</tr>
<tr>
<td class="listtd">
<img src="${pageContext.request.contextPath }/client/images/icon1.png" width="15" height="10" />
<a href="${pageContext.request.contextPath }/client/modifyuserinfo.jsp">用户信息修改</a>
</td>
</tr>
<tr>
<td class="listtd">
<img src="${pageContext.request.contextPath }/client/images/icon2.png" width="15" height="10" />
<a href="${pageContext.request.contextPath}/findOrderByUser">订单查询</a>
</td>
</tr>
<tr>
<td class="listtd">
<img src="${pageContext.request.contextPath }/client/images/icon3.png" width="15" height="10" />
<a href="${pageContext.request.contextPath}/logout" οnclick="javascript:return confirm_logout()">用户退出</a>
</td>
</tr>
</table>
</td>
<td>
<div style="text-align:right; margin:5px 10px 5px 0px">
<a href="${pageContext.request.contextPath }/index.jsp">首页</a>
>
我的帐户 > 欢迎
</div>
<table cellspacing="0" class="infocontent">
<tr>
<td style="padding:20px"><p>
<img src="${pageContext.request.contextPath }/client/images/myad.jpg" width="631" height="436" />
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<jsp:include page="foot.jsp" />
</body>
</html>
2.用户退出界面如图4所示:
图4 用户退出界面
<html>
<head>
<title>电子书城</title>
<link rel="stylesheet" href="${pageContext.request.contextPath }/client/css/main.css"
type="text/css" />
<script type="text/javascript">
//退出确认框
function confirm_logout() {
var msg = "您确定要退出登录吗?";
if (confirm(msg)==true){
return true;
}else{
return false;
}
}
</script>
</head>
<body class="main">
<jsp:include page="head.jsp" />
<jsp:include page="menu_search.jsp" />
<div id="divpagecontent">
<table width="100%" border="0" cellspacing="0">
<tr>
<td width="25%">
<table width="100%" border="0" cellspacing="0" style="margin-top:30px">
<tr>
<td class="listtitle">我的帐户</td>
</tr>
<tr>
<td class="listtd">
<img src="${pageContext.request.contextPath }/client/images/icon1.png" width="15" height="10" />
<a href="${pageContext.request.contextPath }/client/modifyuserinfo.jsp">用户信息修改</a>
</td>
</tr>
<tr>
<td class="listtd">
<img src="${pageContext.request.contextPath }/client/images/icon2.png" width="15" height="10" />
<a href="${pageContext.request.contextPath}/findOrderByUser">订单查询</a>
</td>
</tr>
<tr>
<td class="listtd">
<img src="${pageContext.request.contextPath }/client/images/icon3.png" width="15" height="10" />
<a href="${pageContext.request.contextPath}/logout" οnclick="javascript:return confirm_logout()">用户退出</a>
</td>
</tr>
</table>
</td>
<td>
<div style="text-align:right; margin:5px 10px 5px 0px">
<a href="${pageContext.request.contextPath }/index.jsp">首页</a>
>
我的帐户 > 欢迎
</div>
<table cellspacing="0" class="infocontent">
<tr>
<td style="padding:20px"><p>
<img src="${pageContext.request.contextPath }/client/images/myad.jpg" width="631" height="436" />
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<jsp:include page="foot.jsp" />
</body>
</html>
2.管理员登录页面,输入管理员账号与密码即可登录,登录成功后到达传智书城后台管理系统主页。
图5 管理员登录界面
图6 管理员登录成功
登录核心代码:(后端代码)
public User login(String username, String password) throws LoginException {
try {
//根据登录时表单输入的用户名和密码,查找用户
User user = dao.findUserByUsernameAndPassword(username, password);
//如果找到,还需要确定用户是否为激活用户
if (user != null) {
// 只有是激活才能登录成功,否则提示“用户未激活”
if (user.getState() == 1) {
return user;
}
throw new LoginException("用户未激活");
}
throw new LoginException("用户名或密码错误");
} catch (SQLException e) {
e.printStackTrace();
throw new LoginException("登录失败");
}
}
管理员登录核心代码:
<script type="text/javascript">
function ini(){
document.form1.logonName.focus();
}
</script>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="${pageContext.request.contextPath}/admin/css/Style.css" rel="stylesheet" type="text/css">
</head>
<body οnlοad="ini()">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<table width="452" height="290" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#FFFFFF">
<table width="452" height="290" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="74">
<img src="${pageContext.request.contextPath}/admin/images/logintitle.gif">
</td>
</tr>
<tr>
<td align="center" valign="bottom" background="${pageContext.request.contextPath}/admin/images/loginbg.gif">
<form id="loginAction_home" name="form1" action="${pageContext.request.contextPath}/admin/login/home.jsp" target="_parent" method="post">
<table border="0" align="center" cellpadding="2" cellspacing="0">
<tr align="center">
<td height="30" colspan="2" style="border-bottom: 1px dotted #cccccc">
<strong style="font-size: 14px;">请登录</strong>
</td>
</tr>
<tr>
<td height="30" nowrap>
<font color="000F60"><strong>用户名:</strong> </font>
</td>
<td>
<input type="text" name="logonName" value="" id="logonName" class="text" style="width: 160px;"/>
</td>
</tr>
<tr>
<td height="30" nowrap>
<strong><font color="000F60">密码: </font> </strong>
</td>
<td>
<input type="password" name="logonPwd" id="logonPwd" class="text" style="width: 160px;"/>
</td>
</tr>
<tr>
<td height="30" nowrap colspan="2">
<strong><font color="red"></font> </strong>
</td>
</tr>
<tr>
<td height="30">
</td>
<td>
<input type="submit" name="submit" value="登录" class="buttoninput"/>
<input type="reset" name="reset" value="取消" class="buttoninput"/>
</td>
</tr>
</table>
</form>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" align="center">
</td>
</tr>
<tr>
<td height="23" align="center"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
(二)管理员管理模块
1.管理员管理界面
图7 管理员管理界面
(三)商品信息管理模块
1.添加商品:
图8 添加商品-Java web
`
图9 添加商品-Java web程序设计
图10 添加商品-Java web程序设计
2.删除商品:
删除商品名称为测试修改的商品。
图11 删除商品-测试修改
图12 删除商品-测试修改
3.查询商品:
可以根据价格查询、类别查询、商品编号查询、商品名称查询
图13 查询商品-价格查询
图14查询商品-类别查询
图15 查询商品-商品编号查询
图16 查询商品-商品名称查询
4.修改商品:
可以修改商品名称、价格、数量、类型、描述、图片。
图17 修改商品前
图18修改商品-测试
图19修改商品后
5.增删查改核心代码:
(1)添加商品核心代码:
public class AddProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建javaBean,将上传数据封装.
Product p = new Product();
Map<String, String> map = new HashMap<String, String>();
// 封装商品id
map.put("id", IdUtils.getUUID());
DiskFileItemFactory dfif = new DiskFileItemFactory();
// 设置临时文件存储位置
dfif.setRepository(new File(this.getServletContext().getRealPath(
"/temp")));
// 设置上传文件缓存大小为10m
dfif.setSizeThreshold(1024 * 1024 * 10);
// 创建上传组件
ServletFileUpload upload = new ServletFileUpload(dfif);
// 处理上传文件中文乱码
upload.setHeaderEncoding("utf-8");
try {
// 解析request得到所有的FileItem
List<FileItem> items = upload.parseRequest(request);
// 遍历所有FileItem
for (FileItem item : items) {
// 判断当前是否是上传组件
if (item.isFormField()) {
// 不是上传组件
String fieldName = item.getFieldName(); //获取组件名称
String value = item.getString("utf-8"); //解决乱码问题
map.put(fieldName, value);
} else {
// 是上传组件
// 得到上传文件真实名称
String fileName = item.getName();
fileName = FileUploadUtils.subFileName(fileName);
// 得到随机名称
String randomName = FileUploadUtils
.generateRandonFileName(fileName);
// 得到随机目录
String randomDir = FileUploadUtils
.generateRandomDir(randomName);
// 图片存储父目录
String imgurl_parent = "/productImg" + randomDir;
File parentDir = new File(this.getServletContext()
.getRealPath(imgurl_parent));
// 验证目录是否存在,如果不存在,创建出来
if (!parentDir.exists()) {
parentDir.mkdirs();
}
String imgurl = imgurl_parent + "/" + randomName;
map.put("imgurl", imgurl);
IOUtils.copy(item.getInputStream(), new FileOutputStream(
new File(parentDir, randomName)));
item.delete();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
try {
// 将数据封装到javaBean中
BeanUtils.populate(p, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
ProductService service = new ProductService();
try {
// 调用service完成添加商品操作
service.addProduct(p);
response.sendRedirect(request.getContextPath()
+ "/listProduct");
return;
} catch (AddProductException e) {
e.printStackTrace();
response.getWriter().write("添加商品失败");
return;
}
}
}
①1.添加商品:
public void addProduct(Product p) throws SQLException {
String sql = "insert into products values(?,?,?,?,?,?,?)";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, p.getId(), p.getName(), p.getPrice(),
p.getCategory(), p.getPnum(), p.getImgurl(), p.getDescription());
}
(2)删除商品核心代码:
public class DeleteProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取请求参数,产品id
String id = request.getParameter("id");
ProductService service = new ProductService();
// 调用service完成添加商品操作
service.deleteProduct(id);
response.sendRedirect(request.getContextPath() + "/listProduct");
return;
}
}
①删除订单时,修改商品数量
public void updateProductNum(List<OrderItem> items) throws SQLException {
String sql = "update products set pnum=pnum+? where id=?";
QueryRunner runner = new QueryRunner();
Object[][] params = new Object[items.size()][2];
for (int i = 0; i < params.length; i++) {
params[i][0] = items.get(i).getBuynum();
params[i][1] = items.get(i).getP().getId();
}
runner.batch(DataSourceUtils.getConnection(), sql, params);
}
②根据id删除商品信息:
public void deleteProduct(String id) throws SQLException {
String sql = "DELETE FROM products WHERE id = ?";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, id);
}
}
(3)查询商品核心代码:
public class ListProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建service层的对象
ProductService service = new ProductService();
try {
// 2.调用service层用于查询所有商品的方法
List<Product> ps = service.listAll();
// 3.将查询出的所有商品放进request域中
request.setAttribute("ps", ps);
// 4.重定向到list.jsp页面
request.getRequestDispatcher("/admin/products/list.jsp").forward(
request, response);
return;
} catch (ListProductException e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
return;
}
}
}
②查找所有商品:
public List<Product> listAll() throws SQLException {
String sql = "select * from products";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new BeanListHandler<Product>(Product.class));
}
③根据分类查找:
public List<Product> findByPage(int currentPage, int currentCount,
String category) throws SQLException {
// 要执行的sql语句
String sql = null;
// 参数
Object[] obj = null;
// 如果category不为null,代表是按分类查找
if (!"全部商品".equals(category)) {
sql = "select * from products where category=? limit ?,?";
obj = new Object[] { category, (currentPage - 1) * currentCount,
currentCount, };
} else {
sql = "select * from products limit ?,?";
obj = new Object[] { (currentPage - 1) * currentCount,
currentCount, };
}
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new BeanListHandler<Product>(Product.class),
obj);
④根据id查找商品:
public Product findProductById(String id) throws SQLException {
String sql = "select * from products where id=?";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new BeanHandler<Product>(Product.class), id);
⑤多条件查询:
public List<Product> findProductByManyCondition(String id, String name,
String category, String minprice, String maxprice)
throws SQLException {
List<Object> list = new ArrayList<Object>();
String sql = "select * from products where 1=1 ";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
if (id != null && id.trim().length() > 0) {
sql += " and id=?";
list.add(id);
}
if (name != null && name.trim().length() > 0) {
sql += " and name=?";
list.add(name);
}
if (category != null && category.trim().length() > 0) {
sql += " and category=?";
list.add(category);
}
if (minprice != null && maxprice != null
&& minprice.trim().length() > 0 && maxprice.trim().length() > 0) {
sql += " and price between ? and ?";
list.add(minprice);
list.add(maxprice);
}
Object[] params = list.toArray();
return runner.query(sql, new BeanListHandler<Product>(Product.class),
params);
}
(4)修改商品核心代码:
public class EditProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建javaBean,将上传数据封装.
Product p = new Product();
Map<String, String> map = new HashMap<String, String>();
DiskFileItemFactory dfif = new DiskFileItemFactory();
// 设置临时文件存储位置
dfif.setRepository(new File(this.getServletContext().getRealPath(
"/temp")));
// 设置上传文件缓存大小为10m
dfif.setSizeThreshold(1024 * 1024 * 10);
// 创建上传组件
ServletFileUpload upload = new ServletFileUpload(dfif);
// 处理上传文件中文乱码
upload.setHeaderEncoding("utf-8");
try {
// 解析request得到所有的FileItem
List<FileItem> items = upload.parseRequest(request);
// 遍历所有FileItem
for (FileItem item : items) {
// 判断当前是否是上传组件
if (item.isFormField()) {
// 不是上传组件
String fieldName = item.getFieldName(); //获取组件名称
String value = item.getString("utf-8"); //解决乱码问题
map.put(fieldName, value);
} else {
// 是上传组件
// 得到上传文件真实名称
String fileName = item.getName();
if (fileName != null && fileName.trim().length() > 0) {
fileName = FileUploadUtils.subFileName(fileName);
// 得到随机名称
String randomName = FileUploadUtils
.generateRandonFileName(fileName);
// 得到随机目录
String randomDir = FileUploadUtils
.generateRandomDir(randomName);
// 图片存储父目录
String imgurl_parent = "/productImg" + randomDir;
File parentDir = new File(this.getServletContext()
.getRealPath(imgurl_parent));
// 验证目录是否存在,如果不存在,创建出来
if (!parentDir.exists()) {
parentDir.mkdirs();
}
String imgurl = imgurl_parent + "/" + randomName;
map.put("imgurl", imgurl);
IOUtils.copy(item.getInputStream(),
new FileOutputStream(new File(parentDir,
randomName)));
item.delete();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
try {
// 将数据封装到javaBean中
BeanUtils.populate(p, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
ProductService service = new ProductService();
// 调用service完成修改商品操作
service.editProduct(p);
response.sendRedirect(request.getContextPath() + "/listProduct");
return;
}
}
①修改商品信息,更新数据:
public void editProduct(Product p) throws SQLException {
//1.创建集合并将商品信息添加到集合中
List<Object> obj = new ArrayList<Object>();
obj.add(p.getName());
obj.add(p.getPrice());
obj.add(p.getCategory());
obj.add(p.getPnum());
obj.add(p.getDescription());
//2.创建sql语句,并拼接sql
String sql = "update products " +
"set name=?,price=?,category=?,pnum=?,description=? ";
//判断是否有图片
if (p.getImgurl() != null && p.getImgurl().trim().length() > 0) {
sql += " ,imgurl=?";
obj.add(p.getImgurl());
}
sql += " where id=?";
obj.add(p.getId());
System.out.println(sql);
System.out.println(obj);
//3.创建QueryRunner对象
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
//4.使用QueryRunner对象的update()方法更新数据
runner.update(sql, obj.toArray());
}
(四)传智书城首页
1.传智书城首页界面:
图20 传智书城首页界面
2.传智书城首页面核心代码:
<head>
<title>传智书城</title>
<%-- 导入css --%>
<link rel="stylesheet" href="${pageContext.request.contextPath}/client/css/main.css" type="text/css" />
<!-- 导入首页轮播图css和js脚本 -->
<link type="text/css" href="${pageContext.request.contextPath }/client/css/autoplay.css" rel="stylesheet" />
<script type="text/javascript" src="${pageContext.request.contextPath }/client/js/autoplay.js"></script>
</head>
<body class="main">
<%@include file="head.jsp"%>
<%@include file="menu_search.jsp" %>
<%-- <div id="divad">
<img src="${pageContext.request.contextPath}/client/ad/index_ad.jpg"/>
</div> --%>
<!-- 图书商场首页轮播图 start -->
<div id="box_autoplay">
<div class="list">
<ul>
<li><img src="${pageContext.request.contextPath }/client/ad/index_ad1.jpg" width="900" height="335" /></li>
<li><img src="${pageContext.request.contextPath }/client/ad/index_ad2.jpg" width="900" height="335" /></li>
<li><img src="${pageContext.request.contextPath }/client/ad/index_ad3.jpg" width="900" height="335" /></li>
<li><img src="${pageContext.request.contextPath }/client/ad/index_ad4.jpg" width="900" height="335" /></li>
<li><img src="${pageContext.request.contextPath }/client/ad/index_ad5.jpg" width="900" height="335" /></li>
</ul>
</div>
</div>
<!-- 图书商场首页轮播图 end -->
<div id="divcontent">
<table width="900px" border="0" cellspacing="0">
<tr>
<td width="497">
<img src="${pageContext.request.contextPath}/client/images/billboard.gif" width="497" height="38" />
<table cellspacing="0" class="ctl">
<tr>
<td width="485" height="100%">${n.details }</td>
</tr>
</table>
</td>
<td style="padding:5px 15px 10px 40px">
<table width="100%" border="0" cellspacing="0">
<tr>
<td>
<img src="${pageContext.request.contextPath}/client/images/hottitle.gif" width="126" height="29" />
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0">
<tr>
<c:forEach items="${pList }" var="pArray">
<td style="width:80; text-align:center">
<a href="${pageContext.request.contextPath}/findProductById?id=${pArray[0]}">
<img src="${pageContext.request.contextPath }${pArray[2]}" width="102" height="130" border="0" />
</a>
<br/>
<a href="${pageContext.request.contextPath}/findProductById?id=${pArray[0]}">${pArray[1]}</a>
<%-- <br />作者:${pArray[2] } --%>
</td>
</c:forEach>
</tr>
</table>
</td>
</tr>
</table>
</div>
<%@ include file="foot.jsp" %>
</body>
</html>
(五)公告栏管理模块
1.公告栏管理添加公告界面:
图20 公告管理-添加公告前
图21 添加公告
图22 公告管理-添加公告后
2.公告栏管理添加公告核心代码:
①1.添加公告的servlet:
public class AddNoticeServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
NoticeService nService = new NoticeService();
Notice bean = new Notice();
//获取表单参数
String title = req.getParameter("title");
String details = req.getParameter("details");
//将当前时间设为添加公告的时间
String t = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
bean.setTitle(title);
bean.setDetails(details);
bean.setN_time(t);
//调用addNotice方法
nService.addNotice(bean);
req.getRequestDispatcher("/manager/ListNoticeServlet").forward(req, resp);
}
}
②添加公告:
public void addNotice(Notice n) throws SQLException {
String sql = "insert into notice(title,details,n_time) values(?,?,?)";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, n.getTitle(),n.getDetails(),n.getN_time());
}
3.公告栏管理删除公告界面:
图24 公告管理-删除公告测试前
图25 公告管理删除公告测试后
4.公告栏管理删除公告核心代码:
①删除公告的servlet
public class DeleteNoticeServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
NoticeService nService = new NoticeService();
//获取请求参数,公告id
String n_id = req.getParameter("id");
//调用dao层方法
nService.deleteNotice(n_id); req.getRequestDispatcher("/manager/ListNoticeServlet").forward(req, resp);
}
}
②根据id删除公告
public void deleteNotice(String n_id) throws SQLException {
String sql = "delete from notice where n_id = ?";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, n_id);
}
5.公告栏管理修改公告界面:
图26 公告管理-修改公告端午节前
图27 公告管理-修改端午节公告
图28 公告管理-修改端午节公告后
6.公告栏管理修改公告核心代码:
①修改公告的servlet:
public class UpdateNoticeServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
NoticeService nService = new NoticeService();
Notice bean = new Notice();
//获取表单参数
String title = req.getParameter("title");
String details = req.getParameter("details");
//将当前时间设为添加公告的时间
String t = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
bean.setTitle(title);
bean.setDetails(details);
bean.setN_time(t);
//调用dao层方法
nService.addNotice(bean);
req.getRequestDispatcher("/manager/ListNoticeServlet").forward(req, resp);
}
}
②根据id修改单个公告
public void updateNotice(Notice n) throws SQLException {
String sql = "update notice set title=?,details=?,n_time=? where n_id=?";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, n.getTitle(),n.getDetails(),n.getN_time(),n.getN_id());
}