Thymeleaf拿后台List的值

CustomerController.java

public class CustomerController {

    @Autowired
    CustomerRepository customerRepository;

    @RequestMapping("/")
    public String test(HttpSession session){
        List<Customer> customers = customerRepository.findAll();
        session.setAttribute("customers",customers);
        return "test";
    }
}

CustomerRepository .java

public interface CustomerRepository extends JpaRepository<Customer, Long> {
	List<Customer> findAll();
}

html

<table >
    <tr th:each="c, State : ${session.customers}">
        <td th:text="${c.name}"></td>
        <td th:text="${c.passWord}"></td>
    </tr>
</table>

CustomerController.java

public class CustomerController {

    @Autowired
    CustomerRepository customerRepository;

    @RequestMapping("/")
    public String test(HttpSession session){
        Customer customers = customerRepository.findOne();
        session.setAttribute("customers",customers);
        return "test";
    }
}

CustomerRepository .java

public interface CustomerRepository extends JpaRepository<Customer, Long> {
	Customer findOne();
}

html

<table >
    <tr th:if="${session.customers} != null">
        <td th:text="${session.customers.name}"></td>
        <td th:text="${session.customers.passWord}"></td>
    </tr>
</table>

猜你喜欢

转载自blog.csdn.net/qq_40771292/article/details/106723477