文章目录
- 1.后台管理—添加家居
- 1.需求分析
- 2.程序框架图
- 3.修改FurnDao
- 添加方法add
- 4.修改FurnDaoImpl
- 添加方法add
- 5.单元测试
- FurnDaoTest.java
- 6.修改FurnService
- 添加方法add
- 7.修改FurnServiceImpl
- 添加方法add
- 8.单元测试
- FurnServiceTest.java
- 9.修改furn_add.jsp
- 10.修改FurnServlet
- 添加add方法
- 11.单元测试
- 1.中文乱码问题
- 修改BasicServlet
- 2.表单重复提交问题
- 使用重定向解决,修改FurnServlet
- 3.用户输入格式错误
- 1.前端添加js验证
- 2.后端异常处理
- 2.BeanUtils自动封装javabean
- 1.基本介绍
- 2.代码实例
- 修改FurnServlet.java
- 3.单元测试
- 数据不能为null的异常
- 在javabean中设置默认值,并进行判断
- 4.使用DataUtils封装并处理异常
- 1.DataUtils.java
- 2.修改FurnServlet
- 3.后台管理—删除家居
- 1.需求分析
- 2.程序框架图
- 3.编写dao层
- 1.修改FurnDao
- 添加方法deleteFurnById
- 2.修改FurnDaoImpl
- 添加方法deleteFurnById
- 3.单元测试
- FurnDaoTest.java
- 4.编写service层
- 1.修改FurnService
- 添加方法deleteFurnById
- 2.修改FurnServiceImpl
- 添加方法deleteFurnById
- 3.单元测试
- FurnServiceTest
- 5.修改furn_manage.jsp
- 1.前端删除提示
- 2.超链接设置
- 6.修改FurnServlet
- 添加方法del
- 7.结果展示
- 1.点击删除
- 2.删除之后重定向
- 4.后台管理—修改家居
- 1.需求分析
- 2.程序框架图
- 3.编写dao层
- 1.修改FurnDao
- 添加方法queryFurnById和updateFurn
- 2.修改FurnDaoImpl
- 添加方法queryFurnById和updateFurn
- 3.单元测试
- FurnDaoTest
- 4.编写service层
- 1.修改FurnService
- 添加方法queryFurnById和updateFurn
- 2.修改FurnServiceImpl
- 添加方法queryFurnById和updateFurn
- 3.单元测试
- FurnServiceTest
- 5.编写web层—使数据回显
- 1.修改furn_update.jsp
- 2.修改furn_manage.jsp
- 3.修改FurnServlet
- 添加方法showFurn
- 4.结果展示
- 1.点击铅笔修改
- 2.数据回显
- 6.编写web层—修改家居信息
- 1.修改furn_update.jsp
- 2.修改FurnServlet
- 增加方法updateFurn
- 3.结果展示
- 1.初始修改界面
- 2.准备修改
- 3.修改成功
1.后台管理—添加家居
1.需求分析
2.程序框架图
3.修改FurnDao
添加方法add
//向数据库中添加家居public int add(Furn furn);
4.修改FurnDaoImpl
添加方法add
/*** 添加家居* @return 成功返回1,失败返回-1*/@Overridepublic int add(Furn furn) {String sql = "insert into furn values(null, ?, ?, ?, ?, ?, ?)";//添加家居int update = update(sql, furn.getName(), furn.getMaker(),furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());return update;}
5.单元测试
FurnDaoTest.java
@Testpublic void add() {Furn furn = new Furn(null, "温馨风格盆景架", "蚂蚁家居", new BigDecimal(122), 12, 12, "assets/images/product-image/16.jpg");if (furnDao.add(furn) == 1) {System.out.println("添加成功");} else {System.out.println("添加失败");}}
6.修改FurnService
添加方法add
//2.添加家居public boolean add(Furn furn);
7.修改FurnServiceImpl
添加方法add
/*** 添加家居* @param furn* @return 成功返回true,失败返回false*/@Overridepublic boolean add(Furn furn) {return furnDao.add(furn) == 1 ? true : false;}
8.单元测试
FurnServiceTest.java
@Testpublic void add() {Furn furn = new Furn(null, "温馨风格盆景架", "蚂蚁家居", new BigDecimal(122), 12, 12, "assets/images/product-image/16.jpg");if (furnService.add(furn)) {System.out.println("添加成功");} else {System.out.println("添加失败");}}
9.修改furn_add.jsp
10.修改FurnServlet
添加add方法
/*** 添加家居并重新获取家居信息,请求转发到furn_manage.jsp* @param req* @param resp*/public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {//获取表单信息String name = req.getParameter("name");String maker = req.getParameter("maker");String price = req.getParameter("price");String sales = req.getParameter("sales");String stock = req.getParameter("stock");//封装到bean中Furn furn = new Furn(null, name, maker, new BigDecimal(price), Integer.parseInt(sales), Integer.parseInt(stock), "");//插入到数据库if (furnService.add(furn)) {//插入成功则重新获取家居信息并请求转发list(req, resp);} else {System.out.println("插入失败");}}
11.单元测试
1.中文乱码问题
修改BasicServlet
2.表单重复提交问题
使用重定向解决,修改FurnServlet
3.用户输入格式错误
1.前端添加js验证
<%-- 引入jquery --%><script type="text/javascript" src="script/jquery-3.6.0.min.js"></script><script>//验证表单输入信息$(function () {//给提交按钮绑定事件$("input[value='添加家居']").click(function () {//获取价格数据var price = $("input[name='price']").val();//正则验证var pricePattern = /^(0(\.\d+)?|[1-9]\d*(\.\d+)?)$/;if (!pricePattern.test(price)){alert("价格格式不正确!")return false; //取消提交表单}//获取销量数据var sales = $("input[name='sales']").val();//正则验证var salesPattern = /^(0|[1-9]\d*)$/;if (!salesPattern.test(sales)) {alert("销量格式不正确!")return false;}//获取库存数据var stock = $("input[name='stock']").val();//正则验证var stockPattern = /^(0|[1-9]\d*)$/;if (!stockPattern.test(stock)) {alert("库存格式不正确!")return false;}return true;})})</script>
结果展示
2.后端异常处理
1.修改FurnServlet
2.修改furn_add.jsp
3.结果展示
2.BeanUtils自动封装javabean
1.基本介绍
2.代码实例
修改FurnServlet.java
/*** 添加家居并重新获取家居信息,请求转发到furn_manage.jsp* @param req* @param resp*/public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, InvocationTargetException, IllegalAccessException//使用BeanUtils工具类封装信息Furn furn = new Furn();//使用工具类获取表单的输入信息,并使用反射将其封装到bean中,注意,name必须与bean的属性对应BeanUtils.populate(furn, req.getParameterMap());System.out.println(furn);//插入到数据库if (furnService.add(furn)) {//插入成功则重定向到家居显示的servletresp.sendRedirect(super.getServletContext().getContextPath() + "/manage/furnServlet?action=list");} else {System.out.println("插入失败");}}
3.单元测试
数据不能为null的异常
在javabean中设置默认值,并进行判断
4.使用DataUtils封装并处理异常
1.DataUtils.java
package com.sxs.furns.utils;import org.apache.commons.beanutils.BeanUtils;import java.lang.reflect.InvocationTargetException;
import java.util.Map;/*** @author 孙显圣* @version 1.0*/
public class DataUtils {//封装bean的异常处理public static <T> T copyParamToBean(Map value, T bean){ //bean的类型不确定,所以使用泛型try {BeanUtils.populate(bean, value);//进行异常处理} catch (IllegalAccessException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);}//没有异常再返回beanreturn bean; //由于返回的是bean,所以返回值也应该是bean类型T}
}
2.修改FurnServlet
/*** 添加家居并重新获取家居信息,请求转发到furn_manage.jsp* @param req* @param resp*/public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, InvocationTargetException, IllegalAccessException {//使用工具类封装并处理异常Furn furn = DataUtils.copyParamToBean(req.getParameterMap(), new Furn());//插入到数据库if (furnService.add(furn)) {//插入成功则重定向到家居显示的servletresp.sendRedirect(super.getServletContext().getContextPath() + "/manage/furnServlet?action=list");} else {System.out.println("插入失败");}}
3.后台管理—删除家居
1.需求分析
2.程序框架图
3.编写dao层
1.修改FurnDao
添加方法deleteFurnById
//根据id删除家居public int deleteFurnById(Integer id);
2.修改FurnDaoImpl
添加方法deleteFurnById
/*** 删除家居* @param id* @return 成功返回1,失败返回-1*/@Overridepublic int deleteFurnById(Integer id) {String sql = "delete from furn where id = ?";//删除家居return update(sql, id);}
3.单元测试
FurnDaoTest.java
@Testpublic void deleteFurnById() {if (furnDao.deleteFurnById(13) == 1) {System.out.println("删除成功");} else {System.out.println("删除失败");}}
4.编写service层
1.修改FurnService
添加方法deleteFurnById
//3.根据id删除家居public boolean deleteFurnById(Integer id);
2.修改FurnServiceImpl
添加方法deleteFurnById
/*** 根据id删除家居* @param id* @return 成功返回true,失败返回false*/@Overridepublic boolean deleteFurnById(Integer id) {return furnDao.deleteFurnById(id) == 1 ? true : false;}
3.单元测试
FurnServiceTest
@Testpublic void deleteFurnById(){if (furnService.deleteFurnById(16)) {System.out.println("删除成功");} else {System.out.println("删除失败");}}
5.修改furn_manage.jsp
1.前端删除提示
<%--引入jquery--%><script type="text/javascript" src="script/jquery-3.6.0.min.js"></script><script type="text/javascript">$(function () { //页面加载完毕后执行function(){}//绑定点击事件$("a.deleteCss").click(function () {//获取到要删除的家居的名字var furnName = $(this).parent().parent().find("td:eq(1)").text();//1. confirm方法会弹出一个确认窗口//2. 点击确定,返回true//3. 点击取消,返回falsereturn confirm("你确定要删除【" + furnName + "】?");})})</script>
2.超链接设置
6.修改FurnServlet
添加方法del
/*** 根据获取的id删除家居,获取家居信息并重定向到furn_manage.jsp* @param req* @param resp*/public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {//获取idString id = req.getParameter("id");//根据id删除if (furnService.deleteFurnById(Integer.parseInt(id))) {resp.sendRedirect(super.getServletContext().getContextPath() + "/manage/furnServlet?action=list");} else {System.out.println("删除失败");}}
7.结果展示
1.点击删除
2.删除之后重定向
4.后台管理—修改家居
1.需求分析
2.程序框架图
3.编写dao层
1.修改FurnDao
添加方法queryFurnById和updateFurn
//根据id查询家居public Furn queryFurnById(Integer id);//根据Furn对象修改家居public int updateFurn(Furn furn);
2.修改FurnDaoImpl
添加方法queryFurnById和updateFurn
/*** 根据id查询** @param id* @return 成功返回Furn对象,失败返回null*/@Overridepublic Furn queryFurnById(Integer id) {String sql = "select * from furn where id = ?";return querySingle(sql, Furn.class, id);}/*** 根据furn的id来更新所有记录** @param furn* @return 成功返回1,失败返回-1*/@Overridepublic int updateFurn(Furn furn) {String sql = "update furn set name = ?, maker = ?, price = ?, sales = ?, stock = ?, " +"img_path = ? WHERE id = ?";return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(),furn.getSales(), furn.getSales(), furn.getImgPath(), furn.getId());}
3.单元测试
FurnDaoTest
@Testpublic void queryFurnById() {Furn furn = furnDao.queryFurnById(1);if (furn != null) {System.out.println(furn);} else {System.out.println("查询失败");}}@Testpublic void updateFurn() {Furn furn = new Furn(20, "温馨风格盆景架", "蚂蚁家居", new BigDecimal(122), 12, 12, "assets/images/product-image/16.jpg");if (furnDao.updateFurn(furn) == 1) {System.out.println("修改成功");} else {System.out.println("修改失败");}}
4.编写service层
1.修改FurnService
添加方法queryFurnById和updateFurn
//4.根据id查询家居public Furn queryFurnById(Integer id);//5.根据Furn对象修改家居public boolean updateFurn(Furn furn);
2.修改FurnServiceImpl
添加方法queryFurnById和updateFurn
/*** 根据id查询家居* @param id* @return 成功返回Furn对象,失败返回null*/@Overridepublic Furn queryFurnById(Integer id) {return furnDao.queryFurnById(id);}/*** 根据furn对象的id来更新信息* @param furn* @return 成功返回true,失败返回false*/@Overridepublic boolean updateFurn(Furn furn) {return furnDao.updateFurn(furn) == 1 ? true : false;}
3.单元测试
FurnServiceTest
@Testpublic void queryFurnById() {Furn furn = furnService.queryFurnById(1);if (furn != null) {System.out.println(furn);} else {System.out.println("查询失败");}}@Testpublic void updateFurn() {Furn furn = new Furn(20, "11111", "蚂蚁家居", new BigDecimal(122), 12, 12, "assets/images/product-image/16.jpg");if (furnService.updateFurn(furn)) {System.out.println("更新成功");} else {System.out.println("更新失败");}}
5.编写web层—使数据回显
1.修改furn_update.jsp
2.修改furn_manage.jsp
3.修改FurnServlet
添加方法showFurn
/*** 根据id查询家居信息,将信息请求转发到前端** @param req* @param resp*/public void showFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取idString id = req.getParameter("id");//根据id查询信息Furn furn = furnService.queryFurnById(Integer.parseInt(id));if (furn != null) {//将信息放到request中并请求转发到furn_update.jspreq.setAttribute("furn", furn);req.getRequestDispatcher("/views/manage/furn_update.jsp").forward(req, resp);} else {System.out.println("查询失败");}}
4.结果展示
1.点击铅笔修改
2.数据回显
6.编写web层—修改家居信息
1.修改furn_update.jsp
2.修改FurnServlet
增加方法updateFurn
/*** 读取表单信息,进行修改* @param req* @param resp*/public void updateFurn(HttpServletRequest req, HttpServletResponse resp) throws IOException {//使用工具类将表单信息封装到bean中Furn furn = DataUtils.copyParamToBean(req.getParameterMap(), new Furn());//执行更新操作if (furnService.updateFurn(furn)) {//更新成功则重定向到展示页面resp.sendRedirect(super.getServletContext().getContextPath() + "/manage/furnServlet?action=list");}}