[关闭]
@wangzhuanyun 2017-07-06T15:13:56.000000Z 字数 3071 阅读 420

鸿卓商城——鸿卓分类页面数据封装

作者:王转运

鸿卓国际


1.鸿卓分类页面数据封装调用

leixing.png-104.1kB

model下代码如下:

  1. /**
  2. * 商品类别实体类
  3. * @author john
  4. */
  5. @SuppressWarnings("serial")
  6. public class GoodsTypeInfo extends Model<GoodsTypeInfo> {
  7.  
  8. public static GoodsTypeInfo dao = new GoodsTypeInfo();
  9.  
  10. /**
  11. * 获取上商品类型集合
  12. * @return
  13. */
  14. public List<GoodsTypeInfo> getListGoodsTypeInfo()
  15. {
  16. //返回数据集合
  17. return GoodsTypeInfo.dao.find("select * from xxshop_goods_type ");
  18. }
  19.  
  20. }

创建商品控制类:GoodController.java

controller下代码如下:
添加方法名:getGoodsTypeInfoList

  1. /**
  2. * 获取商品分类
  3. */
  4. public void getGoodsTypeInfoList()
  5. {
  6. try {
  7.  
  8. //商品分类
  9. List GoodsTypeInfoList = GoodsTypeInfo.dao.getListGoodsTypeInfo();
  10. //存入页面
  11. setAttr("GoodsTypeInfoList",GoodsTypeInfoList);
  12.  
  13. render("/category.jsp");
  14.  
  15. }catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18.  
  19. }

category.jsp添加代码如下:

引入JSTL标签库:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

  1. <!--
  2. forEach循环
  3. var 每次循环得到的对象(数据库查询后一行的数据集合)
  4. items ${放入存起来的名字}
  5. -->
  6.  
  7. <c:forEach var="GoodsTypeInfo" items="${GoodsTypeInfoList}" >
  8. <a href="" class="list-group-item">
  9. ${GoodsTypeInfo.type_name} <!--type_name为数据库列名-->
  10. <span class="glyphicon glyphicon-chevron-right" style="float: right;"></span>
  11. </a>
  12. </c:forEach>
  13.  
  14.  
  15. #代码说明:
  16. 页面跳转href 中加入下面这段代码
  17. <%=basePath%>good/getClassListGoodsInfoList?type_name=${GoodsTypeInfo.type_name }&type_id=${GoodsTypeInfo.type_id}
  18.  

2.点击分类跳转商品列表信息页面

list.png-101.6kB

model下代码如下:

  1. /**
  2. * 根据商品类型 获取商品信息
  3. * @param type_id 商品类型ID
  4. * @return
  5. */
  6. public List<GoodsInfo> getListGoodsInfo(long type_id)
  7. {
  8. String sql = "select a.*,(select images_url from xxshop_images where images_type =2 and goods_id=a.goods_id ) images_url from xxshop_goods a where a.type_id = ?";
  9. Object[] obj = new Object[]{type_id};
  10. return GoodsInfo.dao.find(sql,obj);
  11. }

controller下代码如下:
添加方法名:getClassListGoodsInfoList

  1. /**
  2. * 根据商品类型ID 获取商品集合信息
  3. */
  4. public void getClassListGoodsInfoList()
  5. {
  6. try {
  7. /*
  8. * 调用getListGoodsInfo方法
  9. * getParaToLong("type_id") 获取href中传过来的值
  10. */
  11. List<GoodsInfo> GoodsInfoList = GoodsInfo.dao.getListGoodsInfo(getParaToLong("type_id"));
  12. //吧type_name存起来
  13. setAttr("type_name",new String(getPara("type_name").getBytes("iso-8859-1"),"utf-8"));
  14. //吧获取的商品都存起来
  15. setAttr("GoodsInfoList",GoodsInfoList);
  16. //跳转商品列表集合页面
  17. render("/list.jsp");
  18.  
  19. }catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }

list.jsp添加代码如下:

引入JSTL标签库:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

  1. 修改头部商品类型名称为:${type_name}
  2.  
  3. <c:forEach var="GoodsInfo" items="${GoodsInfoList}" >
  4.  
  5. <!--
  6. goodclick
  7. goods_id="${GoodsInfo.goods_id }"
  8. JQUERY使用
  9. -->
  10. &ltdiv class="col-xs-12 goodclick" goods_id="${GoodsInfo.goods_id }" style="background-color: #fff;padding: 10px;box-shadow: 0 9px 25px -8px rgba(0, 0, 0, 0.38);margin-top:5px">
  11.  
  12. <div class="col-xs-4 text-center" style="padding:0px">
  13. //动态放入图片路径
  14. <img src="img/${GoodsInfo.images_url }" style="max-width: 100%">
  15.  
  16. </div>
  17. <div class="col-xs-8 " >
  18. <div style="width: 100%;overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">
  19. ${GoodsInfo.goods_name }//商品名称
  20.  
  21. </div>
  22. <div style="width: 100%;font-size: 12px; color: #888;line-height: 20px;height: 10%">
  23. ${GoodsInfo.goods_title }//商品标题
  24. </div>
  25.  
  26. <div style="width: 100%;text-align: right;">
  27. <span style="color:#00B8F1;font-size: 16px;line-height:30px;margin-right:5px">
  28. ${GoodsInfo.goods_money }//商品价格
  29. </span>
  30. </div>
  31. </div>
  32. </div>
  33. </c:forEach>
  34.  
  35. #添加JQUERY代码:
  36. $(function(){
  37. //点击商品后触发跳转页面
  38. $(".goodclick").click(function(){
  39. location= '<%=basePath%>good/getGoodsInfo?goods_id='+$(this).attr("goods_id");
  40. });
  41. });
  42.  
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注