Shop项目--3. 使用ajax获取商品分类列表,并存在redis中

来源:互联网 发布:淘宝代销货图片签协议 编辑:程序博客网 时间:2024/06/05 05:14

分析:

head.jsp 是显示商品分类的页面,通过 <jsp:include page="/header.jsp"></jsp:include>引用

要想多个页面引用head.jsp时候,都能加载categoryList,所以使用ajax来获取categoryListJson。

并且提高读取效率,使用redis数据库把它存起来。

注意前台页面,字符串拼接时候的双引改为单引。注意前台代码书写正确,提示错误不明显


准备:

jedisUtils工具, redis.proper配置文件,jedis的两各jar包


步骤:

1.在head.jsp页面使用ajax 写出功能链接,回调函数,数据类型

2.编写功能函数,使用jedisUtils获取对象,并连接redis数据库,并判断是否有categoryListJson

3.如果没有,从数据库中查找得出categoryList,使用Gson工具toJson为categoryListJson,并存在refis数据库。

4.ajax的回调函数拿到categoryListJson,for循环进行拼接字符串。并添加到ul中


head.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html><!-- 登录 注册 购物车... --><div class="container-fluid"><div class="col-md-4"><img src="img/logo2.png" /></div><div class="col-md-5"><img src="img/header.png" /></div><div class="col-md-3" style="padding-top:20px"><ol class="list-inline"><li><a href="login.jsp">登录</a></li><li><a href="register.jsp">注册</a></li><li><a href="cart.jsp">购物车</a></li><li><a href="order_list.jsp">我的订单</a></li></ol></div></div><!-- 导航条 --><div class="container-fluid"><nav class="navbar navbar-inverse"><div class="container-fluid"><!-- Brand and toggle get grouped for better mobile display --><div class="navbar-header"><button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="#">首页</a></div><!-- 商品分类列表 ajax--><script type="text/javascript">$(function(){$.post("${pageContext.request.contextPath}/product?method=category",function(data){//[{"cid":"xxx","cname":"xxx"},{},{},{},{}]var content ="";for (var i = 0; i < data.length; i++) {//拼接字符串后,内部双引号必须改为单引号content+="<li><a href='#'>"+data[i].cname+"</a></li>"}$("#categoryUl").html(content);},"json");});</script><div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"><ul class="nav navbar-nav" id="categoryUl"><!-- <li class="active"><a href="product_list.htm">手机数码<span class="sr-only">(current)</span></a></li><li><a href="#">电脑办公</a></li><li><a href="#">电脑办公</a></li><li><a href="#">电脑办公</a></li> --></ul><form class="navbar-form navbar-right" role="search"><div class="form-group"><input type="text" class="form-control" placeholder="Search"></div><button type="submit" class="btn btn-default">Submit</button></form></div></div></nav></div>

ProductServlet

package com.itheima.web.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;import com.itheima.domain.Category;import com.itheima.domain.Product;import com.itheima.service.ProductService;import com.itheima.utils.JedisPoolUtils;import redis.clients.jedis.Jedis;public class ProductServlet extends BaseServlet {//获取商品的分类列表public void category(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {ProductService service = new ProductService();//先从缓存中查询是否有categoryList,没有再从数据库中查询//1.获得jedis对象连接redis数据库Jedis jedis = JedisPoolUtils.getJedis();String categoryListJson = jedis.get("categoryListJson");//2.判断categoryList是否为空if(categoryListJson==null) {System.out.println("缓存没有数据,查找数据库");//准备分类数据List<Category> categoryList = service.findCategory();Gson gson = new Gson();categoryListJson = gson.toJson(categoryList);jedis.set("categoryListJson", categoryListJson);}//有中文,所以要解决乱码问题response.setContentType("text/html;charset=UTF-8");response.getWriter().write(categoryListJson);}//首页动态获取最新商品和热门商品public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {ProductService service = new ProductService();//获取热门商品List<Product> hotProList = service.findHotProduct();//获取最新商品List<Product> newProList = service.findNewProduct();//传递到request域,并转发到index.jsprequest.setAttribute("hotProList", hotProList);request.setAttribute("newProList", newProList);request.getRequestDispatcher("/index.jsp").forward(request, response);}}

ProductService

package com.itheima.service;import java.sql.SQLException;import java.util.List;import com.itheima.dao.ProductDao;import com.itheima.domain.Category;import com.itheima.domain.Product;public class ProductService {//获取热门商品public List<Product> findHotProduct() {ProductDao dao = new ProductDao();List<Product> hotProList = null;try {hotProList = dao.findHotProduct();} catch (SQLException e) {e.printStackTrace();}return hotProList;}//获取最新商品public List<Product> findNewProduct() {ProductDao dao = new ProductDao();List<Product> newProList = null;try {newProList = dao.findNewProduct();} catch (SQLException e) {e.printStackTrace();}return newProList;}//获取商品列表public List<Category> findCategory() {ProductDao dao = new ProductDao();List<Category> categoryList = null;try {categoryList = dao.findCategory();} catch (SQLException e) {e.printStackTrace();}return categoryList;}}

ProductDao

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.Category;import com.itheima.domain.Product;import com.itheima.utils.DataSourceUtils;public class ProductDao {// 获取热门商品public List<Product> findHotProduct() throws SQLException {QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());String sql = "select * from product where is_hot=? limit ?,?";List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), 1,0,9);return query;}//获取最新商品public List<Product> findNewProduct() throws SQLException {QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());//此处注意使用order by pdate desc 时间最大开始选择String sql ="select * from product order by pdate desc limit ?,?";List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), 0,9);return query;}//获取商品分类列表public List<Category> findCategory() throws SQLException {QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());String sql ="select * from category";List<Category> query = runner.query(sql, new BeanListHandler<Category>(Category.class));return query;}}



原创粉丝点击