ssm-学子商城-项目第十天

3.商品展示

调整页面:index.jsp页面上给三级分类添加超级链接

<li><a href="../goods/showSearch.do?categoryId=${computer.id}">${computer.name}</a></li>

调整GoodsController控制器类

给方法添加参数
@RequestMapping("/showSearch.do")
public String showSearch(Integer categoryId){

    return "search";
}

3.1 商品展示-持久层

3.2 商品展示-业务层

3.3 商品展示-控制器层

public String showSearch(Integer categoryId,ModelMap map){
    //调用业务层的方法,返回集合
    List<Goods> goodsList =
    goodsService.getGoodsByCategoryId(categoryId, 0, 12);
    //把集合设置到map
    map.addAttribute("goodsList",goodsList);        

    return "search";
}

3.4 商品展示-页面

在search.jsp页面中显示商品信息

<div id="wrap">
    <c:forEach items="${goodsList}" var="goods">
                <div class="lf box" id="d1">
                    <div class="info">
                    <div class="img pic">
                        <img src="..${goods.image}" alt="" onclick="toItemInfo(${item.id})" />
                    </div>          
                    <div class="describe">
                        <p onclick="toItemInfo(${item.id})">${goods.title}</p>
                        <span class="price"><b>¥</b><span class="priceContent">${goods.price}</span></span>
                        <span class="addCart"><img id="collect" src="../images/search/care.png" alt="" /><a href="javascript:void(0);" class="add_cart">加入购物车</a></span>
                        <!--<span class="succee" style="display: none"> 
                            <img src="/images/search/product_true.png" alt="" /> 
                            <span>已移入购物车</span>
                        </span>-->
                    </div>
                    </div>
                </div>
        </c:forEach>        
            </div>

调整页面:显示分页

<div  align="right" style="margin-right: 80px">
共28中商品,共4页|1 2 3 4

显示共多少条记录 在GoodsMapper接口中定义方法

Integer selectCount(Integer categoryId);

在GoodsMapper.xml文件中,定义select节点,完成查询的功能

<select id="selectCount" resultType="java.lang.Integer">

    select 
        count(*)
    from 
        t_goods;
    where
        category_id=#{categoryId}

</select>

在IGoodsService接口中定义方法

Integer getCount(Integer categoryId);

在GoodsService实现类中,调用持久层的方法,发回值

在控制器方法中showSearch(),调用业务层方法,getCount(),得到记录数,把值设置map中

//在map设置记录数
    map.addAttribute("count",goodsService.getCount(categoryId));

在search.jsp页面上显示记录数

<div  align="right" style="margin-right: 80px">
共${count}种商品,共4页|1 2 3 4

页面分页处理的完整代码

<div  align="right" style="margin-right: 80px">
共${count}种商品,共${pages}页|
<c:forEach var="i" begin="1" end="${pages}">
  <a 
  href="../goods/showSearch.do?categoryId=${categoryId}&page=${i}"
  <c:if test="${currentPage==i}">
  style="color:#ff0000"
  </c:if>
  >
  ${i}
  </a>
</c:forEach>

4.商品详情

4.1 商品详情页-持久层

在GoodsMapper接口中定义方法

Goods selectByGoodsId(Integer id);

在GoodsMapper.xml编写select语句

<select id="selectByGoodsId" resultType="...">

    select
        ....
    from
        t_goods
    where
        id=#{id}

</select>

测试:

4.2 商品详情页-业务层

在IGoodsService 接口中定义方法

Goods getGoodsById(Integer id);

在GoodsService类中实现方法,返回持久层的对象

测试:

4.3 商品详情页-控制器层

/goods/goodsInfo.do
请求参数:goodsId,categoryId
请求方式:GET
响应方式:转发
@
public String goodsInfo(Integer goodsId,Integer categoryId,ModelMap map){
    //1.调用业务层方法 getGoodsById(goodsId),返回goods对象
    //2.把goods对象设置到map中
    //3.return "product_details";
}

把productdetails.html改写成productdetails.jsp

4.4 商品详情页-页面

在search.jsp页面上调整代码:在图片和titile的链接上调用toItemInfo函数: onclick="toItemInfo(${goods.id},${goods.categoryId})"

<script type="text/javascript">
/* 商品详情页  */
function toItemInfo(goodsId,categoryId) {
    if (goodsId) {
        window.location.href="../goods/goodsInfo.do?goodsId="+goodsId+"&categoryId="+categoryId;
    }else {
        alert("商品id不存在");
    }
} 
</script>    

在index.jsp页面上调整代码,给热卖的商品添加链接,到商品详情

<a href="${pageContext.request.contextPath}/goods/goodsInfo.do?goodsId=${goods.id}&categoryId=${goods.categoryId}">
查看详情
</a>

在商品详情页product_details.jsp页面上显示动态数据

图片
<div id="mediumDiv">
        <img width="460px" height="360px" id="mImg" src="..${goods.image}"/>
    </div>

 <!-- 右侧-->
<div class="right_detail lf">
    <!-- 商品名称-->
    <h1>${goods.itemType}</h1>
    <!-- 商品全称-->
    <h3>${goods.title}</h3>
    <!-- 价格部分-->
    <div class="price">
        <div id="pro_price"><b>学员售价:</b><span>¥${goods.price}</span></div>
        <div class="promise">
            <b>服务承诺:</b>
            <span>*退货补运费</span>
            <span>*30天无忧退货</span>
            <span>*48小时快速退款</span>
            <span>*72小时发货</span>
        </div>
    </div>

4.5 商品详情-为您推荐

在GoodsController类的goodsInfo方法中,调用业务层方法getGoodsByCategoryId(),得到装4个商品的List ,把list设置到map中;

//查询该分类的热门商品4个
    List<Goods> goodsList=
            goodsService.getGoodsByCategoryId(categoryId, 0, 4);
    //把goodsList设置到map中
    map.addAttribute("goodsList",goodsList);

在product_details.jsp页面显示数据。

<c:forEach items="${goodsList}" var="goods">
             <div class="detail_1 lf">
                <div class="detail_img1">
                    <img src="..${goods.image}" border="0">
                </div>
                <p>${goods.title}</p>
            </div>    
        </c:forEach>  

购物车

设计表 t_cart

create table t_cart(
    id int auto_increment primary key,
    uid int not null,
    goods_id varchar(200),
    count int,
    created_user varchar(50),
    created_time date,
    modified_user varchar(50),
    modified_time date      
)default charset=utf8;

1.添加购物车

1.1 添加购物车-持久层

新建Cart实体类,然后新建CartMapper接口,在接口中定义方法

void insertCart(Cart cart)

新建CartMapper.xml(拷贝,改名),定义insert节点,完成插入数据功能

<mapper namespace="......CartMapper">
    <insert id="insertCart" parameterType=".......">
        insert into t_cart(
            uid,goods_id,count,
            created_user,created_time,
            modified_user,modified_time
        )values(
           #{uid},#{goodsId},#{count},
           #{createdUser},#{createdTime},
           #{modifiedUser},#{modifiedTime}
        )
    </insert>
</mapper>

测试:

猜你喜欢

转载自blog.csdn.net/sinat_41915844/article/details/80070441