java - thymeleaf 值到 html 选择输入

标签 java spring hibernate jpa thymeleaf

Controller

@GetMapping("/kosik")
public String kosik(Principal principal,Model model){
    User user = userServices.findByEmail(principal.getName());
    Cart cart = cartServices.findCartByUser(user);
    model.addAttribute("produkty",cartItemServices.findAllCartItems(cart));
    model.addAttribute("cart",cartServices.findCartByUser(user));
    model.addAttribute("user",user);
    return "cart";
}

HTML

<ul class="list-group mb-3">
                <li th:each="produkt: ${produkty}" class="list-group-item d-flex justify-content-between lh-condensed">
                    <div>
                        <h6 th:text="${produkt.product.name}" class="my-0"></h6>
                    </div>
                    <span th:text="${produkt.price}" class="d-inline-block"></span><span class="d-inline-block"> €</span>

                    <form th:action="@{/updateCartItem(name=${produkt.product.name})}" th:object="${cartItem}" method="post">
                        <select th:field="*{quantity}">
                            <option th:value="1">1</option>
                            <option th:value="2">2</option>
                            <option th:value="3">3</option>
                            <option th:value="4">4</option>
                            <option th:value="5">5</option>
                        </select>
                        <input type="submit" value="Update" class="btn-sm btn-primary" />
                    </form>
               </li>
            <li th:each="kosik: ${cart}" class="list-group-item d-flex justify-content-between">
                <span>Total Price</span>
                <strong th:text="${kosik.totalPrice}"></strong>
            </li>
        </ul>

一切都在更新,工作正常,但我无法获取当前的购物车数量来选择 th:field,它仍然是 1,我也尝试过 th:selected,但没有工作..

最佳答案

要修复此问题,您需要删除 th:field 并将其替换为 name 和 id 属性。

<select id="quantity" name="quantity">
    <option value="1">1</option>
    ...
    <option value="5">5</option>
    <option value="6" selected="selected">6</option>
     ...
    <option value="10">10</option>
</select>

关于java - thymeleaf 值到 html 选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60403698/

相关文章:

java - 获取sql记录并存储在hashmap中

java - Spring + JSP - 如何将数据库中的值设置为默认输入文本值?

java - 如何使用 CrudRepository 中的 nativeQuery 以键、值的方式获取记录总和?

java - Codename One 主题设计器未启动

java - Spring 根据需要重新加载应用程序上下文

spring - 类型/通用方面是否值得付出努力?

java - Spring 4.2.3 与 Hibernate validator 5.2.2.Final 不起作用

java - Hibernate OneToMany 列表为空

java - 如何在 Hibernate 查询和 Session.get() 中启用 MySQL BINARY 运算符

java - Swift 新手 - 可失败的初始化器