【问题标题】:How to call a link with requestparameter inside loop如何在循环内调用带有请求参数的链接
【发布时间】:2019-09-23 13:28:47
【问题描述】:

我想在 URL 中引用索引, 对于如下所示的控制器, 这样当单击列表中第一项的按钮时, 对应的要调用的URL是:

/product/detailpage/index/0 ->for the first item 
/product/detailpage/index/2 ->for the third item
and so on

如何修改代码和

HTML:

    <tbody>
        <tr th:each="product, iter  : ${list}">
            <td th:text="*{product.id}"></td>
            <td th:text="*{product.name}"></td>
            <td style="text-align: right;">
                <form action="@{/product/detailpage/index/(idx=${iter.index})}">
                    <input type="submit" value="Details" />
                </form>
            </td>
        </tr>
    </tbody>

Java:

@Controller
@RequestMapping("/product/detailpage")
public class ProductDetailPageController
{
    /**
     * It is the developer's responsibility to provide code that handles
     * the concatenation the correct index to this AttributeName,
     * so as to get the the corresponding value from the Model data structure.
     * For example, productDetailAtIndex_0,
     *              productDetailAtIndex_1,
     *              productDetailAtIndex_n
     */
    public static final String sProductDetailPageAttrName = "productDetailAtIndex_";

    @Autowired
    private ProductService productService;

    /**
    * Solution-1
    */
    @RequestMapping(value = "/index/{idx}", method = RequestMethod.GET)
    @ResponseBody

【问题讨论】:

    标签: java spring-boot spring-mvc thymeleaf


    【解决方案1】:

    您想看到/product/detailpage/index/0 而不是/product/detailpage/index/?idx=0?您只需要添加一个与您选择的变量名称匹配的占位符。像这样:

    th:action="@{/product/detailpage/index/{idx}(idx=${iter.index})}"
    

    或用于网址

    th:url="@{/product/detailpage/index/{idx}(idx=${iter.index})}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多