bookStore案例第一篇【部署开发环境、解决分类模块】
来源:互联网 发布:linux terminal 字体 编辑:程序博客网 时间:2024/06/04 19:20
前言
巩固Servlet+JSP开发模式,做一个比较完整的小项目
成果图
该项目包含了两个部分,前台和后台。
前台用于显示
后台用于管理
该项目可分为5个模块来组成:分类模块,用户模块,图书模块,购买模块,订单模块。
搭建环境
建立包结构
导入开发包
前台分帧页面
- index.jsp【没有body标签的】
<frameset rows="25%,*"> <frame src="${pageContext.request.contextPath}/client/head.jsp"/> <frame src="${pageContext.request.contextPath}/client/body.jsp"/> </frameset>
- head.jsp
<body style="text-align: center"><h1>欢迎来到购物中心</h1>
body是空白的jsp页面
效果:
后台分帧页面
- manager.jsp【嵌套了framset标签,也是没有body标签的】
<frameset rows="25%,*"> <frame src="${pageContext.request.contextPath}/background/head.jsp"/> <frameset cols="15%,*"> <frame src="${pageContext.request.contextPath}/background/left.jsp"/> <frame src="${pageContext.request.contextPath}/background/body.jsp"/> </frameset></frameset>
- head.jsp
<body style="text-align: center"><h1>后台管理</h1>
- left.jsp
<a href="#">分类管理</a><br><br><a href="#">图书管理</a><br><br><a href="#">订单管理</a><br><br>
body.jsp是空白的
效果:
分帧的文件夹目录结构
值得注意的是:
- 文件夹的名字不能使用“manager”,不然会出现:403 Access Denied错误
- frameset标签是可以嵌套的,分列用“cols”,分行用“rows”
导入工具类和方法的代码
- 过滤中文乱码数据
- HTML转义
- DAOFactory
- JDBC连接池
- UUID工具类
- c3p0.xml配置文件
这些代码都可以在我的博客分类:复用代码中找到!
分类模块
首先,我们来做分类模块吧
创建实体Category
private String id; private String name; private String description; //各种setter、getter
在数据库创建表
CREATE TABLE category ( id VARCHAR(40) PRIMARY KEY, name VARCHAR(10) NOT NULL UNIQUE , description VARCHAR(255));
编写CategoryDAO
/** * 分类模块 * 1:添加分类 * 2:查找分类 * 3:修改分类 * * * */public class CategoryImpl { public void addCategory(Category category) { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "INSERT INTO category (id, name, description) VALUES(?,?,?)"; try { queryRunner.update(sql, new Object[]{category.getId(), category.getName(), category.getDescription()}); } catch (SQLException e) { throw new RuntimeException(e); } } public Category findCategory(String id) { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "SELECT * FROM category WHERE id=?"; try { Category category = (Category) queryRunner.query(sql, id, new BeanHandler(Category.class)); return category; } catch (SQLException e) { throw new RuntimeException(e); } } public List<Category> getAllCategory() { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "SELECT * FROM category"; try { List<Category> categories = (List<Category>) queryRunner.query(sql, new BeanListHandler(Category.class)); return categories; } catch (SQLException e) { throw new RuntimeException(e); } }}
测试DAO
public class demo { @Test public void add() { Category category = new Category(); category.setId("2"); category.setName("数据库系列"); category.setDescription("这是数据库系列"); CategoryImpl category1 = new CategoryImpl(); category1.addCategory(category); } @Test public void find() { String id = "1"; CategoryImpl category1 = new CategoryImpl(); Category category = category1.findCategory(id); System.out.println(category.getName()); } @Test public void getAll() { CategoryImpl category1 = new CategoryImpl(); List<Category> categories = category1.getAllCategory(); for (Category category : categories) { System.out.println(category.getName()); } }}
抽取成DAO接口
public interface CategoryDao { void addCategory(Category category); Category findCategory(String id); List<Category> getAllCategory();}
后台页面的添加分类
- 在超链接上,绑定显示添加分类的页面
<a href="${pageContext.request.contextPath}/background/addCategory.jsp" target="body">添加分类</a>
- 显示添加分类的JSP页面
<form action="${pageContext.request.contextPath}/CategoryServlet?method=add" method="post"> 分类名称:<input type="text" name="name"><br> 分类描述:<textarea name="description"></textarea><br> <input type="submit" value="提交"></form>
- 处理添加分类的Servlet
if (method.equals("add")) { try { //把浏览器带过来的数据封装到bean中 Category category = WebUtils.request2Bean(request, Category.class); category.setId(WebUtils.makeId()); service.addCategory(category); request.setAttribute("message", "添加分类成功!"); } catch (Exception e) { request.setAttribute("message","添加分类失败"); e.printStackTrace(); } request.getRequestDispatcher("/message.jsp").forward(request, response); }
- 效果:
后台页面的查看分类
- 在超链接上,绑定处理请求的Servlet
else if (method.equals("look")) { List<Category> list = service.getAllCategory(); request.setAttribute("list", list); request.getRequestDispatcher("/background/lookCategory.jsp").forward(request, response); }
- 显示分类页面的JSP
<c:if test="${empty(list)}"> 暂时还没有分类数据哦,请你添加把</c:if><c:if test="${!empty(list)}"> <table border="1px"> <tr> <td>分类名字</td> <td>分类描述</td> <td>操作</td> </tr> <c:forEach items="${list}" var="category"> <tr> <td>${category.name}</td> <td>${category.description}</td> <td> <a href="#">删除</a> <a href="#">修改</a> </td> </tr> </c:forEach> </table></c:if>
- 效果:
1 0
- bookStore案例第一篇【部署开发环境、解决分类模块】
- 第一篇 搭建开发环境
- 第一篇 开发环境的搭建
- 第一篇:android开发环境的搭建
- web 开发第一篇 环境配置
- android学习第一篇 开发环境搭建
- 第一篇 开发环境的建立
- Android开发环境部署 及 常见问题解决
- 黑莓开发第一篇:开发环境的搭建
- 第一篇-配置Android开发环境(图)[转帖]
- 基于GBT28181:SIP协议组件开发-----------第一篇环境搭建
- Kinect For Windows2.0开发第一篇之搭建环境
- Kinect For Windows2.0开发第一篇之搭建环境
- 第一篇 Android驱动开发环境搭建之一 -- 虚拟机搭建
- 基于GBT28181:SIP协议组件开发-----------第一篇环境搭建
- RN学习第一篇-开发环境的配置
- Activiti第一篇【介绍、配置开发环境、快速入门】
- python 学习笔记 第一篇:开发环境搭建
- Flex tree 遍历
- Spring事件驱动模型
- 你是如何学习 Linux 编程的?
- servlet监听完成统计在线人数,显示在线人员列表(附源码)
- Flex tree 展开任意想展开的节点
- bookStore案例第一篇【部署开发环境、解决分类模块】
- JSON解析-开源jsoncpp---动态链接库生成
- Servlet(11)HttpServletResponse、HttpServletRequest详解及中文乱码处理
- 经验分享之使用pdfbox截取pdf(java)
- NPM小结
- Flex中利用flexpaper查看文件
- springmvc实体类类型转换
- python基础教程学习笔记 第二章 列表和元组
- mysql 远程连接