Spring Boot&Thymeleaf,将变量传递给控制器

huangapple go评论76阅读模式
英文:

Spring Boot & Thymeleaf,passing variables to the controller

问题

以下是您提供的内容的翻译部分:

控制器代码:

@PostMapping("/buyproduct")
public String buyProduct(@RequestParam int count, @RequestParam long id) {
    Product product = productRepository.findById(id);
    int activeCount = product.getCount();
    if (activeCount - count < 0) {
        return "redirect:/";
    }
    product.setCount(activeCount -= count);
    productRepository.save(product);
    return "redirect:/buyproductsuccessful";
}

HTML代码:

<div class="col-12 col-md-6 col-lg-4" th:each="element : ${products}">
    <div class="card">
        <img class="card-img-top" src="https://picsum.photos/362/180" alt="Card image cap">
        <div class="card-body">
            <h5 style="text-align: center" class="card-title" th:text="${element.getName()}"></h5>
            <span th:text="${element.getCost()}"></span>
            <span th:text="${element.getCount()}"></span>
            ...
        </div>
    </div>
    <form action="#" th:action="@{/buyproduct}" th:object="${element}" method="post">
        <!-- Modal Header -->
        <div class="modal-header">
            <h4 class="modal-title" style="color: #368819"></h4>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <!-- Modal body -->
        <div class="modal-body">
            <div>
                <input type="hidden" name="id" th:value="*{getId()}">
                <label><b>数量</b></label><br>
                <input class="col-4" type="number" min="1" value="1" name="count" required><br><br>
                <label><b></b></label><br>
                <input class="col-12" type="text"><br><br>
                <label><b></b></label><br>
                <input class="col-12" type="date"><br><br>
                <label><b>CVV/CVC</b></label><br>
                <input class="col-4" type="text"><br><br>
            </div>
        </div>
        <!-- Modal footer -->
        <div class="modal-footer">
            <button type="submit" class="btn btn-success"></button>
            <button type="button" class="btn btn-danger" data-dismiss="modal"></button>
        </div>
    </form>
</div>

请注意,翻译中的代码可能会存在格式不一致或错误。如果您需要准确的代码翻译,请在实际工作中进行验证。

英文:

I have a controller that processes the purchase of a product,it takes two parameters,count - the quantity of the product and id - the ID of the product that the user buys.

@PostMapping(&quot;/buyproduct&quot;)
public String buyProduct(@RequestParam int count,@RequestParam long id){
Product product = productRepository.findById(id);
int activeCount = product.getCount();
if (activeCount-count&lt;0){
return &quot;redirect:/&quot;;
}
product.setCount(activeCount-=count);
productRepository.save(product);
return &quot;redirect:/buyproductsuccessful&quot;;
}

In index.html I have listed all available products in the database using Thymeleaf.

&lt;div class=&quot;col-12 col-md-6 col-lg-4&quot; th:each=&quot;element : ${products}&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;img class=&quot;card-img-top&quot; src=&quot;https://picsum.photos/362/180&quot; alt=&quot;Card image cap&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 style=&quot;text-align: center&quot; class=&quot;card-title&quot; th:text=&quot;${element.getName()}&quot;&lt;/h5&gt;
&lt;span th:text=&quot;${element.getCost()}&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span th:text=&quot;${element.getCount()}&quot;&gt;&lt;/span&gt; &lt;/p&gt;
...

Each product has its own button,when clicked, a popup opens in which you need to enter the quantity of the purchased product and Bank data (I don't process them, I don't Need them yet), and there is also a hidden field in which I want to put the product ID so that it can be passed to the controller later.

&lt;form action=&quot;#&quot; th:action=&quot;@{/buyproduct}&quot; th:object=&quot;${element}&quot; method=&quot;post&quot;&gt;
&lt;!-- Modal Header --&gt;
&lt;div class=&quot;modal-header&quot;&gt;
&lt;h4 class=&quot;modal-title&quot; style=&quot;color: #368819&quot;&gt;&lt;/h4&gt;
&lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot;&gt;&amp;times;&lt;/button&gt;
&lt;/div&gt;
&lt;!-- Modal body --&gt;
&lt;div class=&quot;modal-body&quot;&gt;
&lt;div&gt;
&lt;input type=&quot;hidden&quot; name=&quot;id&quot; th:value=&quot;*{getId()}&quot;&gt;
&lt;label&gt;&lt;b&gt;რაოდენობა&lt;/b&gt;&lt;/label&gt;&lt;br&gt;
&lt;input class=&quot;col-4&quot; type=&quot;number&quot; min=&quot;1&quot; value=&quot;1&quot; name=&quot;count&quot; required&gt;&lt;br&gt;&lt;br&gt;
&lt;label&gt;&lt;b&gt;&lt;/b&gt;&lt;/label&gt;&lt;br&gt;
&lt;input class=&quot;col-12&quot; type=&quot;text&quot;&gt;&lt;br&gt;&lt;br&gt;
&lt;label&gt;&lt;b&gt;&lt;/b&gt;&lt;/label&gt;&lt;br&gt;
&lt;input class=&quot;col-12&quot; type=&quot;date&quot;&gt;&lt;br&gt;&lt;br&gt;
&lt;label&gt;&lt;b&gt;CVV/CVC&lt;/b&gt;&lt;/label&gt;&lt;br&gt;
&lt;input class=&quot;col-4&quot; type=&quot;text&quot;&gt;&lt;br&gt;&lt;br&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- Modal footer --&gt;
&lt;div class=&quot;modal-footer&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-danger&quot; data-dismiss=&quot;modal&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;

But Thymeleaf passes the id of the first product every time,what is my mistake?

答案1

得分: 0

问题出在 Bootstrap 模态框作用域上。

<button style="margin-left: 40%" type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal">
<div class="modal" id="myModal">

修复后的代码如下。

<button style="margin-left: 40%" type="button" class="btn btn-success" data-toggle="modal" th:attr="data-target='&#39;#id-&#39; + ${element.getId()}'">
<div class="modal" th:id="'id-' + ${element.getId()}">
英文:

The problem was with Bootstrap modal scopes.

&lt;button style=&quot;margin-left: 40%&quot; type=&quot;button&quot; class=&quot;btn btn-success&quot; data-toggle=&quot;modal&quot; data-target=&quot;#myModal&quot;&gt;
&lt;div class=&quot;modal&quot; id=&quot;myModal&quot;&gt;

After fix its looks like this.

&lt;button style=&quot;margin-left: 40%&quot; type=&quot;button&quot; class=&quot;btn btn-success&quot; data-toggle=&quot;modal&quot; th:attr=&quot;data-target=&#39;#id-&#39; + ${element.getId()}&quot;&gt;
&lt;div class=&quot;modal&quot; th:id=&quot;&#39;id-&#39; + ${element.getId()}&quot;&gt;

huangapple
  • 本文由 发表于 2020年10月21日 21:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64464159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定