使用thymelaf的OneToMany关系?如何使用thymelaf从表单中获取对象列表



这些是我的实体:

@Entity
@Data
public class InvoiceInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long primaryKey;

@OneToMany(mappedBy = "invoiceInfo",cascade = CascadeType.ALL)
private List<Product> listOfProducts=new ArrayList<>();
}
@Entity
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productId;
private String productName;

@ManyToOne
@JoinColumn(name = "invoice_info_primary_key")
private InvoiceInfo invoiceInfo;

我应该如何从百里香表格中获得我在发票实体中提到的产品列表作为输入?

请求DTO

public class ClaimsRequestDto {
private int id; //come from controller or user id 
private List<Integer> beneficiariesList; // I get Id of selected beneficeries from beneficieriesList // 
}

控制器

model.addAttribute("beneficiariesList", beneficiariesResponseDtoList);
model.addAttribute("id", user.getId());
model.addAttribute("claimsRequestDto ", new ClaimsRequestDto ());

表单

<form method="POST" th:action="@{/path}" th:object="${claimsRequestDto}">
<input type="hidden" th:field="*{id}" th:value="${id}"/>
<div class="form-row">
<div class="form-group col-md-8">    
<ul>
<th:block th:each="beneficieriesIndex : ${beneficieriesList}">
<li>
<input type="checkbox"
th:name="beneficiariesList"
th:value="${beneficieriesIndex.id}"/>
<label th:text="${beneficieriesIndex.firstName+' '+beneficieriesIndex.middleName+' '+beneficieriesIndex.lastName}"></label>
</li>
</th:block>
</ul>
</div>
<div class="form-group col-md-3">
<button type="submit" class="btn btn-primary btn-block btn-flat">Request</button>
</div>
</div>
</form>

我试图发送Id列表,但你可以按实体而不是Id 尝试

<th:value="${beneficieriesIndex.id}"/> to <th:value="${beneficieriesIndex}"/>
<th:name="beneficiariesList"> => on rquest dto

最新更新