ssm-购物车实现4(添加)-jt-web

    //利用post传值
    function addCart(){
        var url = "http://www.jt.com/cart/add/${item.id}.html";
        document.forms[0].action = url; //js设置提交链接
            document.forms[0].submit(); //js表单提交
    }
    
package com.jt.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jt.common.po.Cart;
import com.jt.web.service.CartService;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    //http://www.jt.com/cart/show.html
    //跳转购物车
    @RequestMapping("/show")
    public String show(Model model){
        //根据userId查询购物车信息
        Long userId=7L;
        List cartList=cartService.findCartByUserId(userId);
        //将cartList数据保存到request对象中
        model.addAttribute("cartList", cartList);
        return "cart";
    }
    //http://www.jt.com/cart/add/562379.html
    //实现购物车新增
    @RequestMapping("/add/{itemId}")
    public String saveCart(@PathVariable Long itemId, Cart cart){
        Long userId=7L;
        cart.setItemId(itemId);
        cart.setUserId(userId);
        cartService.saveCart(cart);
        //重定向到购物车展现页面
        return "redirect:/cart/show.html";
        }
}
package com.jt.web.service;

import java.util.List;

import com.jt.common.po.Cart;

public interface CartService {

    List findCartByUserId(Long userId);

    void saveCart(Cart cart);

}

package com.jt.web.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.common.po.Cart;
import com.jt.common.service.HttpClientService;
import com.jt.common.vo.SysResult;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private HttpClientService httpClient;
    private static ObjectMapper objectMapper=new ObjectMapper();

    @SuppressWarnings("unchecked")
    @Override
    public List findCartByUserId(Long userId) {
        String url="http://cart.jt.com/cart/query/"+userId;
        String resultJSON = httpClient.doGet(url);
        List cartList=null;
        try {
            SysResult sysResult = objectMapper.readValue(resultJSON, SysResult.class);
            cartList=(List) sysResult.getData();
            
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return cartList;
    }

    /**
     * 如果一个对象中有100个属性问怎么传参数
     */
    @Override
    public void saveCart(Cart cart) {
        String url="http://cart.jt.com/cart/save";
        Mapparams=new HashMap();
        params.put("userId",cart.getUserId()+"" );
        params.put("itemId", cart.getItemId()+"");
        params.put("itemTitle",cart.getItemTitle() );
        params.put("itemImage",cart.getItemImage() );
        params.put("itemPrice",cart.getItemPrice()+"" );
        params.put("num", cart.getNum()+"");
        httpClient.doPost(url, params);
        //理论上需要对返回值进行处理,如果201报错,通过js告知用户,暂时没有实现
        
    }
}

你可能感兴趣的:(ssm-购物车实现4(添加)-jt-web)