WEB21_多条件查询、attr和prop的区别和分页的实现

一、实现多条件查询

在这里插入图片描述
具体实现代码参考JavaWeb基础系列(九)商城分页功能
VO :(value object) ,值对象
通常用于业务层之间的数据传递,和PO一样也是仅仅包含数据而已。但应是抽象出的业务对象,可以和表对应,也可以不,这根据业务的需要.
Java中数据库模糊查询写法
这里主要对一些重要问题做记录:
在这里插入图片描述
在这里插入图片描述
这里我们发现list.jsp没有获取到所有类别信息,这个时候,我们需要获取所有类别数据放到request域,然后转发到JSP,让JSP来进行数据显示
第一次访问这个页面就需要这个数据,故在com.itheima.web.AdminProductListServlet中就在把所有类别数据获取并上传到request域中
在这里插入图片描述
我们可以看到结果:第一次访问这个list.jsp文件是拥有这个所有类别数据
在这里插入图片描述
我们接着开始进行模糊搜索测试:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我们发现搜索结束后,所有类别数据丢失,并且搜索的数据发生丢失,不符合我们正常的用户体验,这是因为点击搜索按钮后进入com.itheima.web.AdminSearchProductListServlt程序执行中,这个程序没有获取所有类别数据,所以所有类别数据发生丢失
在这里插入图片描述
如果需要所有类别数据回显,就需要去数据库中获取所有类别转发到request域中
com.itheima.web.AdminSearchProductListServlt代码

package com.itheima.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.itheima.domain.Category;
import com.itheima.domain.Product;
import com.itheima.service.AdminProductService;
import com.itheima.vo.Condition;

public class AdminSearchProductListServlt extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置对客户端请求和数据库取值时的编码,不指定的话使用iso-8859-1。(只解决POST乱码) 
		request.setCharacterEncoding("UTF-8");
		//1.收集表单数据
		Map<String, String[]> properties = request.getParameterMap();
		//2.将散装的查询数据封装到一个VO实体中
		Condition condition = new Condition();
		try {
			BeanUtils.populate(condition, properties);
		} catch (Exception e) {
			System.out.println(e);
		}
		//3.将实体传递给service层
		AdminProductService service = new AdminProductService();
		List<Product> productList = null;
		try {
			productList = service.findProductListByConditon(condition);
		} catch (SQLException e) {
			System.out.println(e);
		}
		
		//4.我们刷新页面发现,商品类别处,没有数据显示,说明没有获取到数据库中所有类别数据
		//我们需要获取到此数据,存到request域
		List<Category> categoryList = null;
		try {
			categoryList = service.findAllCategory();
		} catch (Exception e) {
			System.out.println(e);
		}
		request.setAttribute("categoryList", categoryList);
		
		request.setAttribute("condition", condition);
		
		request.setAttribute("productList", productList);
		
		//将request域数据转发到list.jsp
		request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

这样修改以后,我们测试效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我们发现所有类别数据的回显成功,但是,会发现我们搜索以后,所选的是否热门以及商品类别应该保留记录,这就需要我们使用JS来处理
在这里插入图片描述

$(function(){
				$("#isHot option[value='${condition.isHot}']").prop("selected",true);
				$("#cid option[value='${condition.cid}']").prop("selected",true);
			});

还需要注意id设置
在这里插入图片描述
完成后,我们再继续测试:
在这里插入图片描述
在这里插入图片描述
我们发现是否热门以及商品分类,没有默认的回显,刚刚JS代码没有生效,这是为什么呢?
经过检查,原来是自己的jquery库忘记导入,导致写入js代码无效,故小伙伴注意开始操作就要导入库

<script language="javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script>

在这里插入图片描述
之后继续测试:
在这里插入图片描述
在这里插入图片描述
我们发现成功,这里小伙伴可能会遇到这种错误:
在这里插入图片描述
这里错误就是HTML中元素id,未与vo中Condition中的成员变量保持一致导致的
在这里插入图片描述

二、分页的后台代码实现

Web层
在这里插入图片描述
Service层:

package com.itheima.service;

import java.sql.SQLException;
import java.util.List;

import com.itheima.dao.AdminProductDao;
import com.itheima.dao.ProductDao;
import com.itheima.domain.Product;

public class ProductService {
	//读取所有商品信息
	public List<Product> findAllProduct() throws SQLException {
		//因为没有复杂的业务,直接传递请求到dao层
		ProductDao dao = new ProductDao();
		return dao.findAllProduct();
	}	
}

Dao层:

package com.itheima.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.itheima.domain.Product;
import com.itheima.utils.DataSourceUtils;

public class ProductDao {

	public List<Product> findAllProduct() throws SQLException {
		//DbUtils类(org.apache.commons.dbutils.DbUtils)主要负责装载驱动、关闭连接的常规工作
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from product";
		List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class));		
		return productList;
	}	
}

访问http://localhost:8080/WEB21/productList地址可得:
在这里插入图片描述
别傻乎乎访问http://localhost:8080/WEB21/product_list.jsp(可能就我傻┭┮﹏┭┮)
因为你已经采用Servlet技术,动态的显示产品信息,所以就需要执行servlet程序,也就是ProductListServlet程序,ProductListServlet对应的url是/productList,在Servlet程序里,将产品信息从数据库中读取上来,存放到request域,然后采用转发,转发到/product_list.jsp,利用JSP进行动态显示
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、分页的后台代码实现

开始将这里request域pageBean修改好
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个时候,前台页面进行修改
在这里插入图片描述
在这里插入图片描述
但是没有上下页箭头
在这里插入图片描述
前台页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />

<style>
body {
	margin-top: 20px;
	margin: 0 auto;
	width: 100%;
}

.carousel-inner .item img {
	width: 100%;
	height: 300px;
}
</style>
</head>

<body>


	<!-- 引入header.jsp -->
	<jsp:include page="/header.jsp"></jsp:include>


	<div class="row" style="width: 1210px; margin: 0 auto;">
		<div class="col-md-12">
			<ol class="breadcrumb">
				<li><a href="#">首页</a></li>
			</ol>
		</div>
		
		<%-- <c:forEach items="${productList}" var="product"> --%>
		<!-- productList已经存到了pageBean -->
		<c:forEach items="${pageBean.productList}" var="product">
			<div class="col-md-2" style="height:250px">
				<a href="product_info.htm"> <img src="${pageContext.request.contextPath }/${product.pimage}"
					width="170" height="170" style="display: inline-block;">
				</a>
				<p>
					<a href="product_info.html" style='color: green'>${product.pname}</a>
				</p>
				<p>
					<font color="#FF0000">商城价:&yen;${product.shop_price}</font>
				</p>
			</div>
		</c:forEach>
	</div>

	<!--分页 -->
	<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
		<ul class="pagination" style="text-align: center; margin-top: 10px;">
			<!-- 上一页 -->
			<!-- 判断当前页是否是第一页 -->
			<c:if test="${pageBean.currentPage==1 }">
				<li class="disabled">
					<a href="javascript:void(0);" aria-label="Previous">
						<span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			</c:if>
			<c:if test="${pageBean.currentPage!=1 }">
				<li>
					<!-- pageBean.currentPage-1--上一页 -->
					<a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage-1}" aria-label="Previous">
						<span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			</c:if>	
			
			<!-- 分页显示商品数据 -->
			<c:forEach begin="1" end="${pageBean.totalPage}" var="page">
				<!-- 判断当前页 -->
				<!-- 是当前页,此页再点击无效,active这个类一般用在导航条中当前高亮的栏目,或者选项卡中当前活动着的选项 -->
				<c:if test="${pageBean.currentPage==page}">
					<li class="active">
						<a href="javascript:void(0);">${page}</a>
					</li>
				</c:if>
				<!-- 判断当前页 -->
				<!-- 不是当前页,此页点击,进行显示当前页商品数据-->
				<c:if test="${pageBean.currentPage!=page}">
					<!-- <li> 标签定义列表项目 -->
					<li>
						<%-- ?currentPage=${page}-方便将当前页参数传到后台 --%>
						<a href="${pageContext.request.contextPath}/productList?currentPage=${page}">${page}</a>
					</li>
				</c:if>
			</c:forEach>
			<!-- 判断当前页是否是最后一页 -->
			<c:if test="${pageBean.currentPage==pageBean.totalPage }">
				<!-- disabled 相等于禁止跳转-->
				<li class="disabled">
					<a href="javascript:void(0);" aria-label="Next"> 
						<span aria-hidden="true">&raquo;</span>
					</a>
				</li>
			</c:if>
			<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
				<li>
					<!-- pageBean.currentPage+1 - 后一页 -->
					<a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage+1}" aria-label="Next"> 
						<span aria-hidden="true">&raquo;</span>
					</a>
				</li>
			</c:if>
			
		</ul>
	</div>
	<!-- 分页结束 -->

	<!--商品浏览记录-->
	<div
		style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;">

		<h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4>
		<div style="width: 50%; float: right; text-align: right;">
			<a href="">more</a>
		</div>
		<div style="clear: both;"></div>

		<div style="overflow: hidden;">

			<ul style="list-style: none;">
				<li
					style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
					src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
			</ul>

		</div>
	</div>


	<!-- 引入footer.jsp -->
	<jsp:include page="/footer.jsp"></jsp:include>

</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值