プログラミング逆引き辞典

~ 多言語対応のプログラミングレシピ ~

SpringBoot ThymeleafでList型のオブジェクトを受け渡すサンプルコード

事前準備:ドメインクラスに対応するテーブルを作成しておくこと
 
O/RマッパーとしてMyBatisを使用している
設定方法が不明な場合は下記参照
 
MyBatis Generatorの設定方法と実行
 
 

■Customer.java ※ドメインクラス

package com.example.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.stereotype.Component;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Component
@Entity
@Table(name="customer")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Customer {
    @Id
    private Integer id;

    private String name;

    private String createDay;

 
 


■CustomerController.java

package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.example.domain.Customer;
import com.example.dto.CustomerListDto;
import com.example.service.CustomerService;

@Controller
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @Autowired
    CustomerListDto customerListDto;

    @ModelAttribute
    CustomerListDto setFormDto() {
        return new CustomerListDto();
    }

    @GetMapping
    String getCustomer(Model model) {
        //customerテーブルのレコードを取得
        List<Customer> customerList = customerService.getCustomerList();

        //CustoemrのリストをcustomerListDtoの「customersフィールド」にセット
        customerListDto.setCustomers(customerList);

        //Modelオブジェクトにセット
        model.addAttribute("customerListDto", customerListDto);

        return "index";
    }

    @PostMapping("update")
    String postCustomer() {
            //
            // 更新時の処理
            //
        return "redirect:/";
    }
}

 
 


■CustomerService.java

package com.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.domain.Customer;
import com.example.domain.CustomerExample;
import com.example.mybatis.mapper.CustomerMapper;

@Service
public class CustomerService {
    @Autowired
    Customer customer;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    CustomerExample customerExample;

    public List<Customer> getCustomerList() {
        //O/RマッパーはMyBatisを使用
        //レコードを取得(select * from customer;)
        List<Customer> resultList = customerMapper.selectByExample(customerExample);
        return resultList;
    }
}

 
 


■CustomerListDto.java

package com.example.dto;

import java.util.List;

import org.springframework.stereotype.Component;

import com.example.domain.Customer;

import lombok.Data;

@Data
@Component
public class CustomerListDto {
    private List<Customer> customers;
}

 
 


■index.html

<!DOCTYPE html>
<html xmlns:th="http:www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form th:action="@{/update}" th:object="${customerListDto}" method="post">
        <table>
            <tr>
                <td>
                    ID
                </td>
                <td>
                    名前
                </td>
                <td>
                    作成日
                </td>
            </tr>
            <!--* 「${customerListFormDto}」の「*{customers}」の要素数ぶん、繰り返す *-->
            <tr th:each="customer, st : *{customers}">
                <td>
                    <!--* 「th:each」のステータス変数「${st.index}」で0から順に「customerListFormDto」の値を取得 プリプロセッシングで事前評価  *-->
                    <input type="text" th:name="${st.count}" th:field="*{customers[__${st.index}__].id}">
                </td>
                <td>
                    <input type="text" th:name="${st.count}" th:field="*{customers[__${st.index}__].name}">
                </td>
                <td>
                    <input type="text" th:name="${st.count}" th:field="*{customers[__${st.index}__].createDay}">
                </td>
            </tr>
        </table>
        <input type="submit" value="更新">
    </form>
    <p th:text="${#calendars.format(today,'dd MMMM yyyy')}"></p>
</body>
</html>