毕业实习项目-10

资源:

链接: https://pan.baidu.com/s/1pjx4cZX_5xQNmnMbUy2R6w .
提取码:00a3

搜索模块-小类别搜索

思路

前台:

  1. 新建一个 productList.jsp文件(资源已给)
  2. 循环遍历 productList.jsp文件中的<li>标签,显示搜索到的商品集合
  3. left.jsp文件中设置一下小类别的访问路径

后台:

  1. ProductServlet

  2. 接受参数smallTypeId、page

  3. 非空判断
    空,return

  4. 调用service查询当前页商品集合(当前页、每页数量、smallTypeId)

  5. 调用service查询商品总数(smallTypeId

  6. 通过PageUtil生成pageCode("product?action=findProductListBySmallTypeId&smallTypeId="+smallTypeId,商品总数、当前页、每页数量)

  7. 存request作用域(当前页商品集合、pageCode、pageName)

  8. 请求转发跳转productList.jsp

实操

  1. 循环遍历 productList.jsp文件中的<li>标签

productList.jsp文件代码:

...
		<div id="main" class="wrap">
			<div class="lefter">
				<jsp:include page="common/left.jsp" />
			</div>

			<div class="main">
				<div class="product-list">
					<ul class="product clearfix">
					
					<!-- 循环遍历 productList.jsp文件中的<li>标签,显示搜索到的商品集合 -->
					<c:forEach items="${productList }" var="product">
					
						<li>
							<dl>
								<dt>
									<a href="product?id=${product.id }" ><img src="${product.proPic }"/></a>
								</dt>
								<dd class="title">
									<a href="product?id=${product.id }" >${
    
    product.name }</a>
								</dd>
								<dd class="price">
									¥${
    
    product.price }
								</dd>
							</dl>
						</li>
						
					</c:forEach>
					
					
					</ul>
					<div class="clear"></div>
					<div class="pager">
						<ul class="clearfix">${
    
    pageCode}</ul>
					</div>
				</div>
			</div>
...
  1. left.jsp文件中设置一下小类别的访问路径

left.jsp文件修改代码:

...
<h2>商品分类</h2>
			<dl>
			
			<!-- 循环遍历<dt><dd>标签 -->
			<c:forEach items="${bigTypeList}" var="bigType" >
			
				<dt>${
    
    bigType.name}</dt>
				
			<!-- 查询大类别的同时,查询当前大类别下的小类别集合 -->
					<c:forEach items="${bigType.smallTypeList}" var="smallType" >
					
				<!--(小类别搜索) 此处加一个链接即可 -->
						<dd><a href="product?action=findProductListBySmallTypeId&smallTypeId=${smallType.id }">${
    
    smallType.name}</a></dd>
								
					</c:forEach>
				
			</c:forEach>
			
			</dl>
		</div>
		
		<div class="spacer"></div>
		<div class="last-view">
			<h2>最近浏览</h2>
...
  1. 修改ProductServlet.java文件代码

ProductServlet.java文件更新代码

...
	//判断用户行为
		String action = request.getParameter("action");
		if("findProductListBySmallTypeId".equals(action)){
    
    
			
			//小类别搜索
			findProductListBySmallTypeId(request,response);//ctrl+1生成一下方法
			
		}/*else if(){
			
			//大类别搜索
			
		}else if(){
			
			//关键词搜索
			
		}*/else{
    
    
			
			//商品详情
			details(request,response); //details报红创建一下方法
		}

	}

	
	//小类别搜索
	private void findProductListBySmallTypeId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接受参数smallTypeId、page
		String smallTypeIdStr = request.getParameter("smallTypeId");
		String pageStr = request.getParameter("page");
		
		//非空判断
		if(StringUtil.isEmpty(smallTypeIdStr)){
    
    
			
			//空,return
			return;
		}
		
		if(StringUtil.isEmpty(pageStr)){
    
    
			
			pageStr = "1";
		}
		
		Integer page = Integer.parseInt(pageStr);
		Integer smallTypeId = Integer.parseInt(smallTypeIdStr);
		
		//调用service查询当前页商品集合(当前页、每页数量、smallTypeId)
		List<Product> list = productService.findProductListBySmallTypeId(page,8,smallTypeId);//报红生成一下方法
		
		//调用service查询商品总数(smallTypeId)
		int total = productService.findProductTotalBysmallTypeId(smallTypeId);//报红生成一下方法
		
		//通过PageUtil生成pageCode(product?action=findProductListBySmallTypeId&smallTypeId=${smallType.id },商品总数、当前页、每页数量)
		String pageCode = PageUtil.getPageCode("product?action=findProductListBySmallTypeId&smallTypeId="+smallTypeId, total, page, 8);
		
		//存request作用域(当前页商品集合、pageCode、pageName)
		request.setAttribute("productList", list);
		request.setAttribute("pageCode", pageCode);
		request.setAttribute("pageName", "商品列表");
		
		//请求转发跳转productList.jsp
		request.getRequestDispatcher("productList.jsp").forward(request, response);
	}
...
  1. ProductServiceImpl.java文件添加代码

ProductServiceImpl.java文件更新代码

...
		//执行查询
		product = qr.query(conn, sql ,new BeanHandler<>(Product.class),params);//BeanHandler报红导一下包
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return product;
	}

	/**
	 * 调用service通过小类别Id查询当前页商品集合(当前页、每页数量、smallTypeId)
	 */
	@Override
	public List<Product> findProductListBySmallTypeId(Integer page, int pageSize, Integer smallTypeId) {
    
    
		
		// TODO Auto-generated method stub
		
		//new一个list
		List<Product> list = new ArrayList<Product>();
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select * from t_product where smallTypeId = ? order by hotTime desc limit ?,?";
		
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		Object[] Params = {
    
    smallTypeId,(page-1)*pageSize,pageSize};
		//执行查询,得到商品集合,再放入List中

		list = qr.query(conn, sql ,new BeanListHandler<>(Product.class),Params);
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return list;
	}


	/**
	 * 调用service通过小类别Id查询商品总数(smallTypeId)
	 */
	@Override
	public int findProductTotalBysmallTypeId(Integer smallTypeId) {
    
    
		// TODO Auto-generated method stub
		
		int total = 0;
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
			
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select count(*) from t_product where smallTypeId = ?";
			
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		Object[] Params = {
    
    smallTypeId};
		//执行查询,得到商品集合,再放入List中
		Long num = qr.query(conn, sql ,new ScalarHandler<>(),Params);
		total = num.intValue();
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return total;
	}

}
}

结果

经过以上的步骤,小类别搜索就完成啦,通过点击红框内的小类别按钮,就可以显示分属不同小类别的商品,这里我点击了连衣裙:
小类别搜索

搜索模块-大类别搜索

大类别搜索模块与小类别搜索模块类似。老师留作思考自己完成。功能是点击下图红框部分能按大类别搜索商品。
(已完成,这里我点击了数码一栏,贴出结果图)
大类别搜索

搜索模块-关键词搜索

思路

前台:

1.在top.jsp文件中设置提交表单路径

后台:

  1. ProductServlet

  2. 接受参数keyword、page

  3. 非空判断

  4. 调用service查询当前页商品集合(当前页、每页数量、keyword

  5. 调用service查询商品总数(keyword

  6. 通过PageUtil生成pageCode("product?action=findProductListByKeyword&keyword="+keyword,商品总数、当前页、每页数量)

  7. 存request作用域(当前页商品集合、pageCode、pageName)

  8. 请求转发跳转 productList.jsp

实操

  1. top.jsp文件中设置提交表单路径

top.jsp文件相关代码:

...
			<!--用户注销-->
			<a href="user?action=logout">注销</a>
			
			<a href="register.jsp">注册</a>
			
			<a href="comment?action=findCommentList">留言板</a>	
			
		</c:if>
		
			<form action="product" method="post">
				<input type="hidden" name="action" value="findProductListByKeyword" />
				<input type="text" name="keyword" value="" />
				<input type="submit" value="搜索" /><br />
				<div id="suggest" style="width: 200px"></div>
			</form>
			
			
		</div>
	
		<div class="navbar">
			<ul class="clearfix">
				<li class="current"><a href="index">首页</a></li>
...
  1. 修改ProductServlet.java文件代码

ProductServlet.java文件更新代码(变动较多,贴出除了拓展包以外的所有代码)

...
/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    
    
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	
	private ProductService productService = new ProductServiceImpl();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("utf8");
		
		//判断用户行为
		String action = request.getParameter("action");
		if("findProductListBySmallTypeId".equals(action)){
    
    
			
			//小类别搜索
			findProductListBySmallTypeId(request,response);//ctrl+1生成一下方法
			
		}else if("findProductListByBigTypeId".equals(action)){
    
    
			
			//大类别搜索
			findProductListByBigTypeId(request,response);
			
		}else if("findProductListByKeyword".equals(action)){
    
    
			
			//关键词搜索
			findProductListByKeyword(request,response);
		}else{
    
    
			
			//商品详情
			details(request,response); //details报红创建一下方法
		}

	}

	
	/**
	 * //大类别搜索
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void findProductListByBigTypeId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接受参数smallTypeId、page
				String bigTypeIdStr = request.getParameter("bigTypeId");
				String pageStr = request.getParameter("page");
				
				//非空判断
				if(StringUtil.isEmpty(bigTypeIdStr)){
    
    
					
					//空,return
					return;
				}
				
				if(StringUtil.isEmpty(pageStr)){
    
    
					
					pageStr = "1";
				}
				
				Integer page = Integer.parseInt(pageStr);
				Integer bigTypeId = Integer.parseInt(bigTypeIdStr);
				
				//调用service查询当前页商品集合(当前页、每页数量、smallTypeId)
				List<Product> list = productService.findProductListByBigTypeId(page,8,bigTypeId);//报红生成一下方法
				
				//调用service查询商品总数(smallTypeId)
				int total = productService.findProductTotalByBigTypeId(bigTypeId);//报红生成一下方法
				
				//通过PageUtil生成pageCode(product?action=findProductListBySmallTypeId&smallTypeId=${smallType.id },商品总数、当前页、每页数量)
				String pageCode = PageUtil.getPageCode("product?action=findProductListByBigTypeId&bigTypeId="+bigTypeId, total, page, 8);
				
				//存request作用域(当前页商品集合、pageCode、pageName)
				request.setAttribute("productList", list);
				request.setAttribute("pageCode", pageCode);
				request.setAttribute("pageName", "商品列表");
				
				//请求转发跳转productList.jsp
				request.getRequestDispatcher("productList.jsp").forward(request, response);
	}


	/**
	 * 关键词搜索
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void findProductListByKeyword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接受参数keyword、page
		String keyword = request.getParameter("keyword");
		String pageStr = request.getParameter("page");
		
		//非空判断
		if(StringUtil.isEmpty(pageStr)){
    
    
			
			pageStr = "1";
			
		}
		
		
		Integer page = Integer.parseInt(pageStr);
		//调用service查询当前页商品集合(当前页、每页数量、keyword)
		List<Product> list = productService.findProductListByKeyword(page,8,keyword);
		
		//调用service查询商品总数(keyword)
		int total = productService.findProductTotalByKeyword(keyword);
		
		//通过PageUtil生成pageCode(product?action=findProductListByKeyword&keyword="+keyword,商品总数、当前页、每页数量)
		String pageCode = PageUtil.getPageCode("product?action=findProductListByKeyword&keyword="+keyword, total, page, 8);
		
		//存request作用域(当前页商品集合、pageCode、pageName)
		request.setAttribute("productList", list);
		request.setAttribute("pageCode", pageCode);
		request.setAttribute("pageName", "商品列表");
		
		//请求转发跳转 productList.jsp
		request.getRequestDispatcher("productList.jsp").forward(request, response);
	}


	//小类别搜索
	private void findProductListBySmallTypeId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接受参数smallTypeId、page
		String smallTypeIdStr = request.getParameter("smallTypeId");
		String pageStr = request.getParameter("page");
		
		//非空判断
		if(StringUtil.isEmpty(smallTypeIdStr)){
    
    
			
			//空,return
			return;
		}
		
		if(StringUtil.isEmpty(pageStr)){
    
    
			
			pageStr = "1";
		}
		
		Integer page = Integer.parseInt(pageStr);
		Integer smallTypeId = Integer.parseInt(smallTypeIdStr);
		
		//调用service查询当前页商品集合(当前页、每页数量、smallTypeId)
		List<Product> list = productService.findProductListBySmallTypeId(page,8,smallTypeId);//报红生成一下方法
		
		//调用service查询商品总数(smallTypeId)
		int total = productService.findProductTotalBysmallTypeId(smallTypeId);//报红生成一下方法
		
		//通过PageUtil生成pageCode(product?action=findProductListBySmallTypeId&smallTypeId=${smallType.id },商品总数、当前页、每页数量)
		String pageCode = PageUtil.getPageCode("product?action=findProductListBySmallTypeId&smallTypeId="+smallTypeId, total, page, 8);
		
		//存request作用域(当前页商品集合、pageCode、pageName)
		request.setAttribute("productList", list);
		request.setAttribute("pageCode", pageCode);
		request.setAttribute("pageName", "商品列表");
		
		//请求转发跳转productList.jsp
		request.getRequestDispatcher("productList.jsp").forward(request, response);
	}

	//商品详情
	private void details(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接收参数
		String id = request.getParameter("id");
		
		//非空判断
		if(StringUtil.isEmpty(id)){
    
    
			
			//空则无操作
			return;
		}
		
		//不空,调用service通过id查询商品详情product 
		Product product = productService.findProductById(Integer.parseInt(id)); //findProductById报红生成一下方法,跳转的文件中将parseInt参数改为id
		
		/**
		 * 最近浏览功能
		 */
		
		//从Session中获取最近浏览商品集合
		List<Product> recentProductList = ((List<Product>) request.getSession().getAttribute("recentProductList"));
		
		//非空判断
		if(recentProductList == null){
    
    
			
			//空,创建集合
			recentProductList = new LinkedList<>();
		}
		
		//写一个标记
		boolean flag = true;
		
		//不为空,
		//循环遍历最近浏览商品集合
		//先判断是不是浏览同一件商品
		for(Product recentProduct : recentProductList){
    
    
			
			if(Integer.parseInt(id) == recentProduct.getId()){
    
    
				
				//是同一件商品,改一下标记
				flag = false;
				
				//先删除原来的浏览记录
				recentProductList.remove(recentProduct);
				
				//再把浏览的这一件商品放在集合最前。
				recentProductList.add(0, recentProduct);
				break;
			}
			
		}
		
		if(flag){
    
    
			
			//不是
			//再判断是否有4个商品(最近浏览最多显示四个)
			if(recentProductList.size() == 4){
    
    
				
				//有4个商品,移除最后一个,把浏览的这个商品放在最前
				recentProductList.remove(3);
				recentProductList.add(0, product);
			}else{
    
    
				
				//如果没有4个商品,就把浏览的这个商品放在最前
				recentProductList.add(0, product);
			}
			
		}
		//存Session,覆盖集合
		request.getSession().setAttribute("recentProductList", recentProductList);
		
		//详情不需要一直保留在会话中,考虑服务器性能,所以将pageName、changePage和product存在作用域request中
		
		//用户访问商品详情时标题商品公告
		request.setAttribute("pageName", "商品");
		request.setAttribute("changePage", "product/product.jsp");
		request.setAttribute("product", product);
		
		//请求转发跳转details.jsp
		request.getRequestDispatcher("details.jsp").forward(request, response);
	}

}

  1. ProductServiceImpl.java文件添加代码(将大类别搜索的代码一并贴出)
...
		Object[] Params = {
    
    smallTypeId};
		//执行查询,得到商品集合,再放入List中
		Long num = qr.query(conn, sql ,new ScalarHandler<>(),Params);
		total = num.intValue();
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return total;
	}

	
	//调用service通过关键词查询当前页商品集合(当前页、每页数量、keyword)
	@Override
	public List<Product> findProductListByKeyword(Integer page, int pageSize, String keyword) {
    
    
		// TODO Auto-generated method stub
		
		//new一个list
		List<Product> list = new ArrayList<Product>();
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select * from t_product where name like ? order by hotTime desc limit ?,?";
		
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		Object[] Params = {
    
    "%"+keyword+"%",(page-1)*pageSize,pageSize};
		//执行查询,得到商品集合,再放入List中

		list = qr.query(conn, sql ,new BeanListHandler<>(Product.class),Params);
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return list;
	}


	//调用service通过关键词查询商品总数(keyword)
	@Override
	public int findProductTotalByKeyword(String keyword) {
    
    
		// TODO Auto-generated method stub

		int total = 0;
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
			
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select count(*) from t_product where name like ?";
			
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		Object[] Params = {
    
    "%"+keyword+"%"};
		//执行查询,得到商品集合,再放入List中
		Long num = qr.query(conn, sql ,new ScalarHandler<>(),Params);
		total = num.intValue();
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return total;
	}

	
	//调用service通过大类别Id查询当前页商品集合(当前页、每页数量、bigTypeId)
	@Override
	public List<Product> findProductListByBigTypeId(Integer page, int pageSize, Integer bigTypeId) {
    
    
		// TODO Auto-generated method stub

		//new一个list
		List<Product> list = new ArrayList<Product>();
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select * from t_product where bigTypeId = ? order by hotTime desc limit ?,?";
		
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		Object[] Params = {
    
    bigTypeId,(page-1)*pageSize,pageSize};
		//执行查询,得到商品集合,再放入List中

		list = qr.query(conn, sql ,new BeanListHandler<>(Product.class),Params);
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return list;
	}


	//调用service通过大类别Id查询商品总数(bigTypeId)
	@Override
	public int findProductTotalByBigTypeId(Integer bigTypeId) {
    
    
		// TODO Auto-generated method stub

		int total = 0;
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
			
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select count(*) from t_product where bigTypeId = ?";
			
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		Object[] Params = {
    
    bigTypeId};
		//执行查询,得到商品集合,再放入List中
		Long num = qr.query(conn, sql ,new ScalarHandler<>(),Params);
		total = num.intValue();
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return total;
	}

结果

经过以上的步骤,关键词搜索就完成啦,我搜索了”裙“字:
关键字搜索

猜你喜欢

转载自blog.csdn.net/weixin_42347543/article/details/114118986