7月5日 SSM 周四

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'list.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script><link rel="stylesheet" href="css/index_work.css" type="text/css"></link></head>
  <script type="text/javascript">
    function inShopCar(id){
        $.post(
            "s_inShopCar",
            {"shop.id":id},
            function (obj){
                if(obj==1){
                    alert("加入成功");
                }
            },"json"
        );
    }
  </script>
  <body>
  <button onclick="location='s_list?x=1'" style="float: right">
    进入购物车
  </button>
    <table>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>所属品牌</th>
            <th>描述</th>
            <th>价格</th>
            <th>销量</th>
            <th>买家评分</th>
            <th>操作</th>
        </tr>
        <c:forEach var="s" items="${shopList}">
            <c:if test="${x==1}">
                <c:if test="${s.shopCar!=null}">
                    <tr>    
                        <th>${s.id }</th>
                        <th>${s.name }</th>
                        <th>${s.brand }</th>
                        <th>${s.content }</th>
                        <th>${s.price }</th>
                        <th>${s.num }</th>
                        <th>${s.score }</th>
                    </tr>
                </c:if>
            </c:if>
            <c:if test="${x!=1}">
                <tr>
                    <th>${s.id}</th>
                    <th>${s.name}</th>
                    <th>${s.brand}</th>
                    <th>${s.content}</th>
                    <th>${s.price}</th>
                    <th>${s.num}</th>
                    <th>${s.score}</th>
                    <th>
                        <button onclick="inShopCar(${s.id})">
                            加入购物车
                          </button>
                    </th>
                </tr>
            </c:if>
        </c:forEach>
    </table>
  </body>
</html>
package com.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.dto.Shop;
import com.service.ShopService;

public class Action {
    private ShopService service;
    private List shopList;
    private Shop shop;
    private Integer x;

    public String list(){
        shopList = service.findShopList();
        return "list";
    }
    public void inShopCar() throws IOException{
        int i = service.inShopCar(shop);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.getWriter().print(i);
    }

    public ShopService getService() {
        return service;
    }

    public void setService(ShopService service) {
        this.service = service;
    }

    public List getShopList() {
        return shopList;
    }

    public void setShopList(List shopList) {
        this.shopList = shopList;
    }

    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }


}
package com.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dto.Shop;
import com.dto.ShopCar;

public class ShopDao extends HibernateDaoSupport{

    public List findShopList() {
        // TODO Auto-generated method stub
        return getHibernateTemplate().find("from Shop");
    }

    public int inShopCar(Shop shop) {
        // TODO Auto-generated method stub
        try {
            Shop s = (Shop) getHibernateTemplate().get(Shop.class, shop.getId());
            s.setShopCar(new ShopCar(1,null));
            getHibernateTemplate().update(s);
            return 1;
        } catch (DataAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

}

水深不语,人稳不言

猜你喜欢

转载自blog.csdn.net/helloworld_1996/article/details/80933585