如何在Controller的ModelMap中将$ .ajax数据设置为List <entity>?

时间:2017-03-21 13:14:38

标签: ajax list spring-mvc

我希望实现购物车的简单功能,用户可以看到他们添加到购物车中的商品,当用户点击购买按钮时,他可以在购物车中购买所有商品。

@Controller

@RequestMapping(value = "/shoppingcart",method = RequestMethod.GET)
public String shoppingCart(ModelMap model) {
    //get goods info of the user's shopping cart
    List<ShoppingCartInfo> shoppingCartInfos = shoppingService.findShoppingCardInfoByUserId(getCurrentUserId());
    model.put("shoppingCartInfos",shoppingCartInfos);
    return "shoppingcart.jsp";
}

JSP

<body>
<%@include file="common/navbar.jsp"%>
<div class="container">
    <table id="shoppingCartTable" class="table table-striped table-hover results ellipsis-table">
        <thead>
        <tr>
            <th>编号</th>
            <th>商品标题</th>
            <th>价格</th>
            <th>购买数量</th>
        </tr>
        </thead>
        <tbody>
            //show goods info
            <c:forEach var="shoppingCartInfo" items="${shoppingCartInfos}" varStatus="count">
                <tr>
                    <td >${count.index + 1}</td>
                    <td><a href="${AppContext}item/showItem/${shoppingCartInfo.itemId}">${shoppingCartInfo.itemTitle}</a></td>
                    <td>${shoppingCartInfo.cost}</td>
                    <td>${shoppingCartInfo.itemNum}</td>
                </tr>
                addData(${shoppingCartInfo})
            </c:forEach>
        </tbody>
    </table>
    <div class="col-md-offset-10" style="margin-top: 20px">
        <a href="javascript:void(0)" class="btn btn-raised btn-primary" id="buyAllItemsBtn">购买</a>
        <a href="javascript:history.back(-1);" class="btn btn-raised btn-primary">退出</a>
    </div>

</div>


<%@include file="common/footer.jsp"%>

AJAX

<script type="text/javascript">

$('#shoppingCartTable').DataTable({
    "language": {
        "url": "${AppContext}resources/lang/datatables_zhcn.json"
    },
    "pagingType": "full_numbers"
})

$('#buyAllItemsBtn').click(function () {
    $.ajax({
        type:"POST",
        url:"${AppContext}buyer/buyAllItems",
        /*traditional: true,*/
        data:${shoppingCartInfos},//  **the error is here**
        success:function (result) {
            if(result.resultStatus == "SUCCESS") {

            }else {
                showModal("购买失败",result.resultMsg,"");
            }
        }
    })
})

错误: Uncaught SyntaxError: Unexpected token {

我的问题:

(1)数据:$ {shoppingCartInfos}我想将数据发送到$ {AppContext} buyer / buyAllItems,但它没有成功 (2)$ {shoppingCartInfos}值为[ShoppingCartInfo {shoppingcartId = 4,userId = 22,itemId = 10}],该对象具有List对象的类名,可以将其删除吗?

我不熟悉这些知识,谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

看看JSON stringify。它可能是您所需要的:http://www.hostingadvice.com/how-to/javascript-object-to-string-tutorial/

var data = JSON.stringify({
        infos: ${shoppingCartInfos}
});

$.ajax({
        type:"POST",
        url:"${AppContext}buyer/buyAllItems",
        data:data,

答案 1 :(得分:0)

这是我的错。我重写了ShoppingCartInfo类的toString()方法

@Override
public String toString() {
    return "ShoppingCartInfo{" +
            "shoppingcartId=" + shoppingcartId +
            ", userId=" + userId +
            ", itemId=" + itemId +
            ", itemTitle='" + itemTitle + '\'' +
            ", cost=" + cost +
            ", itemNum=" + itemNum +
            ", createTime=" + createTime +
            '}';
}

删除ShoppingCartInfo是好的