java Spring添加Cookie

rqdpfwrv  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(82)

我尝试在spring Boot 中使用cookie创建一个购物车,但是cookie没有被添加/显示。下面是控制器Map和页面html,你能告诉我我做错了什么吗?
页面截图:

控制器Map:

@GetMapping("/products/addToCart/{id}")
private String addToCart(@PathVariable("id") long productId, HttpServletResponse response) {
    try {
        Cookie browserSessionCookie = new Cookie(Long.toString(productId), Long.toString(1L));
        response.addCookie(browserSessionCookie);
        return "redirect:/products/cart";
    } catch (Exception e) {
        logger.error(e.getMessage());
        return "fail";
    }
}

@GetMapping("/products/cart")
public String showCookies(HttpServletRequest request, Model model) {
    Cookie[] cookies = request.getCookies();
    model.addAttribute("cookies",cookies);
    return "/cart";
}

Thymeleaf产品列表页面:

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>All Products</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
        table, th, td {
            border: 1px solid black;
        }

        td {
            padding: 20px;
        }

        .topnav {
            background-color: #A32638;
            overflow: hidden;
        }

        .topnav a {
            float: left;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 17px;
        }

        .topnav a:hover {
            background-color: #ddd;
            color: black;
        }

        .topnav a.active {
            background-color: #FCB514;
            color: white;
        }
    </style>
</head>
<body>
<div class="topnav">
    <a th:href="@{/}">Home</a>
    <a class="active" th:href="@{/productsList}">Products</a>
    <a th:href="@{/products/cart}">Cart</a>
</div>
<br>
Products&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br>
<br>
<div th:if="${ not#lists.isEmpty(products)}">
    <table>
        <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Category</th>
            <th>Action</th>
        </tr>
        <tr th:each="product : ${products}">
            <td th:text="${product.name}"></td>
            <td th:text="${product.price}"></td>
            <td th:text="${product.category.name}"></td>
            <td>
                <a th:href="@{/products/details/{id}(id=${product.id})}">Details</a>
                &nbsp;&nbsp;&nbsp;
                <a th:href="@{/products/addToCart/{id}(id=${product.id})}">Add To Cart</a>
                &nbsp;&nbsp;&nbsp;
            </td>
        </tr>
    </table>
    <br>
</div>
</body>
</html>

用于显示Cookie的Thmeleaf页面:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<br>
<h1>Show cookies</h1>
<dl th:each="cookie : ${cookies}">
    <dt th:text="${cookie.getName()}"></dt>
    <dd th:text="${cookie.getValue()}"></dd>
</dl>
<h1>Session Data</h1>
<dl th:each="elem : ${sessionElems}">
    <dt th:text="${elem.getName()}"></dt>
    <dd th:text="${elem.getValue()}"></dd>
</dl>
</body>
</html>

谢谢你。

db2dz4w8

db2dz4w81#

解决方案是将创建的cookie的路径设置为购物车页面的路径,如下所示:
browserSessionCookie.setPath("/products/cart");
感谢您的回复!

相关问题