前言

巩固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>
  • 效果:


bookStore案例第一篇【部署开发环境、解决分类模块】的更多相关文章

  1. 第一篇 PHP开发环境搭建以及多站点配置(基于windows 7系统)

    从今天开始,我将用PHP开发一些小的网站,大家知道LAMP(Linux)组合的优势,使PHP受到广大中小企业的喜欢.使PHP与JAVA,ASP三分天下,PHP具有跨平台性,所以在windows一样是可 ...

  2. maven(多个模块)项目 部署 开发环境 问题处理历程【异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE】

    maven(多个模块)项目 部署 开发环境 问题处理历程[异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE] 201 ...

  3. 使用docker-compose来部署开发环境

    docker-compose的作用 docker-comopse可以帮助我们快速搭建起开发环境,比如你可以去把redis,mongodb,rabbitmq,mysql,eureka,configser ...

  4. electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google JavaScript Style Guide代码规范

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  5. ActionBarSherlock学习笔记 第一篇——部署

    ActionBarSherlock学习笔记 第一篇--部署          ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...

  6. 图书管理系统【JavaWeb:部署开发环境、解决分类、图书、前台页面模块】

    前言 巩固Servlet+JSP开发模式,做一个比较完整的小项目. 成果图 该项目包含了两个部分,前台和后台. 前台用于显示 后台用于管理 该项目可分为5个模块来组成:分类模块,用户模块,图书模块,购 ...

  7. Android五天乐(第一天)开发环境的部署,开发流程与调试

    由于项目要求參与无线端开发,本着技多不压身的指导精神,决定依旧从web转攻client! 由于之前自己玩过两个月android(实际上仅仅是做了两个有失水准的demo级app),本来以为这次再来学习将 ...

  8. Eclipse配置安卓开发环境(解决SDK manager下载慢问题)

    Android新手在eclipse搭建安卓开发环境基本都会遇到Android SDK manager下载慢,ADT下载慢的问题,本文将带大家完整的安装一遍开发环境 工具:eclipse     SDK ...

  9. [nRF51822 AK II 教程]第一课,开发环境的配置及背景介绍【转】

    低功耗蓝牙4.0是全新的技术,并不向下兼容,也就是说它和蓝牙3.0.2.0什么的都不能通信的.另外,蓝牙4.0目前的规范只能做外设和主机(智能手机,电脑等)通讯,也就是说你想用一个单模的蓝牙4.0开发 ...

随机推荐

  1. C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件..很很很很重要..),附源码

    前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.com/l2999019/DemoApp 可以Star一下,随意 - - 说点什么.. 本篇..基 ...

  2. Fedora25和win10双系统安装及使问题汇总

    安装问题汇总 1.U盘引导制作后,开机出现":Assuming driver cache: write through" 解决方案:经过排查后,怀疑是U盘启动制作出了问题,后来查阅 ...

  3. 2017多校第10场 HDU 6181 Two Paths 次短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证 ...

  4. TensorFlow框架(1)之Computational Graph详解

    1. Getting Start 1.1 import TensorFlow应用程序需要引入编程架包,才能访问TensorFlow的类.方法和符号.如下所示的方法: import tensorflow ...

  5. Atomic类和CAS

    说Atomic类之前,先聊一聊volatile. 对volatile的第一印象就是可见性.所谓可见性,就是一个线程对共享变量的修改,别的线程能够感知到. 但是对于原子性,volatile是不能保证的. ...

  6. Linux程序设计综合训练之简易Web服务器

    1.功能需求: (1)学习网络套接字编程.HTPP协议.Web服务器等知识: (2)设计一简单Web服务器,提供静态网页浏览服务功能. 2.实现的功能: (1)C语言实现基于socket的Web服务器 ...

  7. 一文教你迅速解决分布式事务 XA 一致性问题

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:腾讯云数据库团队 近日,腾讯云发布了分布式数据库解决方案(DCDB),其最明显的特性之一就是提供了高于开源分布式事务XA的性能.大型 ...

  8. 使用 qemu 搭建内核开发环境

    本文主要介绍在 MacOS 上使用 qemu 搭建 Linux Kernel 的开发环境.(在开始之前需要注意的是,本文中的 Linux 开发环境是一个远程服务器,而 qemu 被安装在本地的 Mac ...

  9. javascript实现jsp页面的打印预览

    1.加载WebBrowser打印预览控件 <OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" heigh ...

  10. Nginx keepalived实现高可用负载均衡详细配置步骤

    Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...