一、实现多条件查询
具体实现代码参考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">商城价:¥${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">«</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">«</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">»</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">»</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>