欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Thymeleaf获取后台List传值

程序员文章站 2022-05-01 23:14:26
...

List

public class CustomerController {

    @Autowired
    CustomerRepository customerRepository;

    @RequestMapping("/")
    public String test(HttpSession session){
        List<Customer> customers = customerRepository.findAll();
        session.setAttribute("customers",customers);
        return "test";
    }
    
}
public interface CustomerRepository extends JpaRepository<Customer, Long> {
	List<Customer> findAll();
}
<table >
    <tr th:each="c, State : ${session.customers}">
        <td th:text="${c.name}"></td>
        <td th:text="${c.passWord}"></td>
    </tr>
</table>

实体

public class CustomerController {

    @Autowired
    CustomerRepository customerRepository;

    @RequestMapping("/")
    public String test(HttpSession session){
        Customer customers = customerRepository.findOne();
        session.setAttribute("customers",customers);
        return "test";
    }
    
}
public interface CustomerRepository extends JpaRepository<Customer, Long> {
	Customer findOne();
}
<table >
    <tr th:if="${session.customers} != null">
        <td th:text="${session.customers.name}"></td>
        <td th:text="${session.customers.passWord}"></td>
    </tr>
</table>
相关标签: thymeleaf