Search code examples
javaspringtuplesthymeleafquerydsl

How i can get tuple value (querydsl) since thymeleaf?


i don'i know how i get the tuple value of queryDSL since thymeleaf

since backEnd i send this value:

List<Tuple> products = productServiceImpl.findProductByFiltersPaginate(null, 0, 1, null);

ModelAndView view = new ModelAndView();
        view.addObject("products",products);
        view.setViewName(ViewConst.MAIN_LAYOUT);
        view.addObject("view","catalog");
        return view;

but in front (thymeleaf) i dont know how to get the values. my code is the below:

<div th:each="product :${products}">
             <h2 th:text="${product}"></h2>
        </div>
    </div>

but i don't know what put after the name of the variable. i already tried these ways: ${product.name}, ${product['name']}, ${product[0]} but none of them works.

if i put only this ${product} it returns me in this format each value

[39, Moto KTM DUKE, /images/products/product39/m_39_0.jpg]

Solution

  • Based on what you replied with, I think these may work:

    product.get(0, Product.class)

    <!-- Note, you have to replace your.package.Product with the actual package -->
    <div th:each="product :${products}" th:with="class=${T(your.package.Product).class}">
      <h2 th:text="${product.get(0, class)}" />
    </div>
    

    product.get(qProduct.title)

    <!-- For this, you need to add qProduct on the model -->
    <div th:each="product :${products}">
      <h2 th:text="${product.get(qProduct.title)}" />
    </div>
    

    You could also possibly use toArray() as well, though I'm not entirely sure how that would turn out:

    <div th:each="product :${products}" th:with="data=${product.toArray()}">
      <h2 th:text="${data[0]}" />
    </div>