后台管理系统编写总结

开发一个后台的商品管理系统,拥有增删改查的功能。主要用到jsp技术,jstl、el语句,以及MVC的思想,加上JDBC工具类的使用。在开发时,思路应该是从jsp页面开始的,从每一个操作键来确定业务功能,然后编写servlet对象,随后再通过service层传入到dao层进行数据库的操作。在编程操作时有很多的小细节与操作难点,笔者将其一点一点都总结了下来,最开始编程的时候思路是很混乱的,但是通过一步一步的排错总结,思路才得以理清,因此万事还是需要有耐心,不管多难的事情一定得静下心来才能找到解决的办法。


1.后台管理系统的商品页面展示

主要思路:

首先我们需要从数据库中拿到所有的商品数据,因此需要定义方法,这个方法最初在dao层中,随后被service调用,然后再被web层调用,如下图所示

代码如下:

AdminProductListservice service = new AdminProductListservice();
		List<Product> productlist = null;
		try {
			
			productlist = service.findAllproduct();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 获得所有的商品的类别数据
		
		List<Category> categorylist = null;
		try {
			categorylist = service.findAllCategory();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("categorylist", categorylist);
		request.setAttribute("productlist", productlist);
		request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);

从数据中拿到商品的类别数据以及商品其他属性的数据,最后存入request域,并且转发到list.jsp中

list.jsp则会接受该数据,通过EL,jstl写成代码来完成最后的显示,代码如下

<c:forEach items="${productlist }" var="pro" varStatus="vs">
							
								<tr onmouseover="this.style.backgroundColor = 'white'"
									onmouseout="this.style.backgroundColor = '#F5FAFE';">
									<td style="CURSOR: hand; HEIGHT: 22px" align="center"
										width="18%">${vs.count }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center"
										width="17%">
										<img width="40" height="45" src="${pageContext.request.contextPath }/${pro.pimage }">
									</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center"
										width="17%">${pro.pname }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center"
										width="17%">${pro.shop_price }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center"
										width="17%">${pro.is_hot==1?"是":"否" }</td>
									<td align="center" style="HEIGHT: 22px"><a
										href="${ pageContext.request.contextPath }/adminUpdateProductUI?pid=${pro.pid}">
											<img
											src="${pageContext.request.contextPath}/images/i_edit.gif"
											border="0" style="CURSOR: hand">
									</a></td>
	
									<td align="center" style="HEIGHT: 22px">
										<a href="javascript:void(0);" onclick="delProduct('${pro.pid}')"> 
											<img src="${pageContext.request.contextPath}/images/i_del.gif"
											width="16" height="16" border="0" style="CURSOR: hand">
										</a>
									</td>
								</tr>
							
							</c:forEach>

需要注意的是request对应的id一定要统一,并且在在下面的循环的名称也要与实体中定义的名称一致。

2.添加商品的操作

主要思路:添加操作的思路一样的从“添加”这个按钮开始进行,但是在添加操作跳转页面时,需要回显商品类别的下拉列表值,因此额外写一个servlet来完成这个操作,和上面获得category的操作一致,最后将值转到add.jsp中即可,这样add.jsp的UI界面就可以显示下拉列表的选择属性了。随后再创建添加操作的servlet对象,代码如下所示

//获取数据
		Map<String,String[]> parameterMap = request.getParameterMap();
		
		//封装数据
		Product product = new Product();
		try {
			BeanUtils.populate(product, parameterMap);
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//此时已经使用beanutils将表单封装完毕
		//手动设置表单中没有的数据
		product.setPid(UUID.randomUUID().toString());
		product.setPimage("products/1/c_0014.jpg");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String pdate = format.format(new Date());
		product.setPdate(pdate);
		product.setPflag(0);
		
		//传递给service层
		AdminProductListservice service = new AdminProductListservice();
		try {
			service.addProduct(product);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//跳转到列表页面   重定向
		response.sendRedirect(request.getContextPath()+"/adminProductList");

注意这里要设置一下表单中没有的数据,最后页面跳转回页面显示的servlet对象文件,这样才能重新加载到新增加的商品信息并且显示在list.jsp上。

3.删除操作

主要思路:删除操作主要是获得需要删除的商品id信息,凭借这个id来删除商品,因此最开始需要接受这个id信息,需要找到“删除”的按钮,然后给它绑定一个点击事件如下所示

function delProduct(pid){
				var isDel = confirm("您确认要删除吗?");
				if(isDel){
					//要删除
					location.href = "${pageContext.request.contextPath}/adminDelProduct?pid="+pid;
				}
			}

页面回弹出提示是否删除,如果删除则将删除的信息的id携带着一起提交到删除操作的servlet对象。删除操作servlet代码如下

//接受需要删除的信息
		String pid = request.getParameter("pid");
		
		AdminProductListservice service = new AdminProductListservice();
		try {
			service.delProduct(pid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//跳转页面
		response.sendRedirect(request.getContextPath()+"/adminProductList");

4.修改商品信息

主要思路:与之前一样需要从“修改”按钮开始着手,还需要回显商品的信息,因此需要额外写一个servlet进行回显信息的操作。

//获得要查询Product的pid
		String pid = request.getParameter("pid");
		//查询商品信息
		AdminProductListservice service = new AdminProductListservice();
		Product product = null;
		try {
			 product = service.findProductBypid(pid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//获得所有的商品的类别数据
		List<Category> categoryList = null;
		try {
			categoryList = service.findAllCategory();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("product", product);
		request.setAttribute("categoryList", categoryList);
		request.getRequestDispatcher("/admin/product/edit.jsp").forward(request,response);

最后一步将request域中的product和categorylist信息都回显到edit.jsp中,下一步才是开始修改的部分。代码如下

request.setCharacterEncoding("UTF-8");

		//1、获取数据
		Map<String, String[]> properties = request.getParameterMap();
		//2、封装数据
		Product product = new Product();
		try {
			BeanUtils.populate(product, properties);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}

		//此位置Product已经封装完毕----将表单的数据封装完毕
		//手动设置表单中没有数据
		//2)、private String pimage;
		product.setPimage("products/1/c_0033.jpg");
		//3)、private String pdate;//上架日期
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String pdate = format.format(new Date());
		product.setPdate(pdate);
		//4)、private int pflag;//商品是否下载 0代表未下架
		product.setPflag(0);

		//3、传递数据给service层
		AdminProductListservice service = new AdminProductListservice();
		try {
			service.updateProduct(product);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		//跳转到列表页面
		response.sendRedirect(request.getContextPath()+"/adminProductList");

同样的也要设置一些表单中没有提交的数据,最后跳转到商品页面展示的servlet上,将数据库的更新信息更新到页面展示上。

5.条件查询

主要思路:查询的条件可以作为一个表单提交到servlet对象中处理,然后转到dao层中做查询,但是因为不确定查询的参数到底是几个,所以在这个地方需要做判断。代码如下:

QueryRunner runner = new QueryRunner(JDBCutils.getDataSource());
		//定义一个存储实际参数的容器
		List<String> list = new ArrayList<String>();
		String sql = "select * from product where 1=1";
		if(condition.getPname()!=null&&!condition.getPname().trim().equals("")){
			sql+=" and pname like ? ";
			list.add("%"+condition.getPname().trim()+"%");
		}
		if(condition.getIsHot()!=null&&!condition.getIsHot().trim().equals("")){
			sql+=" and is_hot=? ";
			list.add(condition.getIsHot().trim());
		}
		if(condition.getCid()!=null&&!condition.getCid().trim().equals("")){
			sql+=" and cid=? ";
			list.add(condition.getCid().trim());
		}
		
		
		List<Product> productlist = runner.query(sql, new BeanListHandler<Product>(Product.class) , list.toArray());
		
		return productlist;

查询的servlet代码如下

request.setCharacterEncoding("UTF-8");

		//1、收集表单数据
		Map<String, String[]> properties = request.getParameterMap();
		//2、将散装的查询数据封装到一个VO实体中
		Condition condition = new Condition();
		try {
			BeanUtils.populate(condition, properties);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		//3、将实体传递给service层
		AdminProductListservice service = new AdminProductListservice();
		List<Product> productlist = null;
		try {
			productlist = service.findProductListByCondition(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		//准备商品类别
		//获得所有的商品的类别数据
		List<Category> categorylist = null;
		try {
			categorylist = service.findAllCategory();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		request.setAttribute("condition", condition);
		
		request.setAttribute("categorylist", categorylist);

		request.setAttribute("productlist", productlist);

		request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值