Day19

作者: 开发猛男 | 来源:发表于2019-05-14 21:59 被阅读0次

完成商品展示功能

案例实现

案例1-展示分类信息

需求描述:加载页面时,请求数据库查找分类信息,返回页面展示
技术实现:
  1. 将导航条独立为一个header.jsp页面,导入到其他需要导航条的页面中。
  2. 编写CategoryServlet 用于处理分类信息展示
  3. head.jsp页面上ajax异步请求CategoryServlet,返回数据List<Category> clist并展示
$(function(){
  var url = "/day18/category?method=findAll";
   //获取导航栏
  var $ul = $("#headUl");
  $.get(url, function(data){
    $(data).each(function(){
      $ul.append($("<li><a href='/day18/product?method=findByPage&cid="+this.cid"&curPage=1'>this.cname</a></li>"))
    },"json")
  })
})

案例2-查询首页上的热门商品和最新商品

需求描述:点击首页,向数据库中查询热门商品并展示
技术分析:
  1. 重写IndexServlet中index方法,实现需求
  2. 查询热门商品和最新商品分别返回List<Product>hlist, nlist;
  3. 将hlist和nlist保存到request域中,请求转发到/jsp/index.jsp
request.setAttribute("hlist",hlist);
request.setAttributte("nlist",nlist);
return "/jsp/index.jsp"

案例3- 查看每个商品的详情

需求描述:点击商品,弹出新页面展示商品详情
技术分析:
  1. 在商品的图片和名称上添加超链接
<a href=
    "${pageContext.request.contextPath}/product?
    method=getInfoById&pid=${p.pid}">${p.pname}
</a>
  1. 编写public String getInfoById(String pid)方法,返回Product对象
  2. 请求转发到/jsp/product_info.jsp
  3. 在前端展示信息

案例4-分类展示商品

需求分析:点击分类,进入分类下的商品列表
技术分析:
  1. 给header.jsp中的每个category标签添加超链接
  2. 点击链接调用ProductServlet中的findByPage(cid,curPage),封装成PageBean对象返回。
//在ProductService中 封装PageBean对象
ProductDao pd = new ProductDaoImpl();
List<Product> plist = pd.findByPage(cid,curPage,pageSize);
// String sql = "select * from product where cid= ? limit ?, ?"
// qr.query(sql, new BeanListHandler<>(Product.class), cid, (curPage-1)*pageSize, pageSize)
int totalCount = pd.getTotalCount(cid);
// String sql = "select count(*) from product where cid = ?"
// return ((Long)qr.query(sql, new ScalaHandler(), cid)).intValue();  
return new PageBean<>(plist, curPage, pageSize, totalCount);

3.请求转发product_list.jsp,展示商品

技术综合

1.页面包含

  • 静态包含
<%@include file="/jsp/header.jsp"%>
  • 动态包含
<jsp:include page="/jsp/header.jsp"%>

2. 缓存ehcache

使用方法
  1. 导入jar包


    图1 需要的jar包
  2. 配置文件,放入src目录下
  3. 使用api
//1.输入配置文件流,创建缓存管理器
// 项目启动时,src目录下文件会被加载进classes目录
//getClassLoader先获取类加载器,再获取该目录下的文件流
CacheManager cm = CacheManager.create
  (CategoryServiceImpl.class.getClassLoader()
       .getResourceAsStream("ehcache.xml"));
    //2.根据名称获取缓存
      Cache cache = cm.getCache("categoryCache");
    //3.通过缓存获取数据 将cache看成一个map集合
      Element element = cache.get("list");
if(element!=null) {
    System.out.println("已有缓存,从缓存中读取");
    clist = (List<Category>)element.getObjectValue();           
}else {
            System.out.println("无缓存,从数据库查询");
        CategoryDao cd = new CategoryDaoImpl();
        clist = cd.findAll();
        Element e = new Element("list", clist);
        cache.put(e);
}

3. js 和 jQuery语法

4. sql中count(*)返回long值 用ScalaHandler()接收

5. 解耦合

对接口的实现类进行修改时,如果实例过多,需要手动修改很多java代码,非常不方便,例如

ProductService ps = new ProductServiceImpl();
//现在我们学习了hibernate,重新编写了 ProductServiceHibImpl()
//则需改写成
ProductService ps = new ProductServiceHibImpl();

每个相关的方法都需要修改,非常麻烦

解决思路:

将接口和实现类写入xml文件,通过工具类读取xml文件,后期只需对xml文件进行修改即可改变实现类。

  1. 编写beans.xml放入src目录下
<?xml version="1.0" encoding="UTF-8"?>
<beans>

    <bean id="ProductService" class="com.itheima.service.impl.ProductServiceImpl"></bean>
    <bean id="CategoryService" class="com.itheima.service.impl.CategoryServiceImpl"></bean>
    <bean id="UserService" class="com.itheima.service.impl.UserServiceImpl"></bean>
    
    
    <bean id="ProductDao" class="com.itheima.dao.impl.ProductDaoImpl"></bean>
    <bean id="CategoryDao" class="com.itheima.dao.impl.CategoryDaoImpl"></bean>
    <bean id="UserDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
    
</beans>
  1. 导入dom4j-1.6.1.jar和jaxen-1.2.0-atlassian-2.jar
  2. 抽取工具类对xml进行解析 获取相关信息
package com.itheima.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class BeanFactory {

    public static Object getBean(String id) {
        try {
            // 1.读取xml文件
            Document doc = new SAXReader().read(BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml"));

            // 2.根据id值获取元素结点 和 响应的值
            Element node = (Element) doc.selectSingleNode("//bean[@id='" + id + "']");
            String value = node.attributeValue("class");

            // 3.返回生成的对象
            return Class.forName(value).newInstance();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

  1. 创建实例时,调用工具类方法
ProductService ps = BeanFactory.getBean("ProductService");

错误

1. 序列化

在使用ehcache的时候,缓存保存在服务器的内存上,如果缓存内容过多,就需要将缓存保存到服务器硬盘上,此时需要将对象进行序列化。即Bean对象 implements Serializable 接口

相关文章

网友评论

      本文标题:Day19

      本文链接:https://www.haomeiwen.com/subject/pwxlaqtx.html