[英]Adding two objects that belong to two different models with similar attribute names in one form using Thymeleaf and Spring MVC
[英]Spring MVC/Thymeleaf - How to insert to two different tables using one input field?
我有一个名字输入字段:
<input th:field="*{fleet.firstName}" class="signup1" type="text" id="fname" name="fname" autofocus="" required=""/>
我有两个表/对象“舰队”和“服务”。 如果百里香仅允许我在每个输入字段中使用一个对象,该怎么办?
我尝试了两次输入th:field,但是没有用。 喜欢:
<input th:field="*{fleet.firstName}" th:field="*{service.firstName}" class="signup1" type="text" id="fname" name="fname" autofocus="" required=""/>
我认为这是不可能的。 您可以为Thymeleaf创建一个DTO对象,并结合您尝试在Web端看到的字段。 然后,将其分成服务层中的数据库表。
就像是:
选项1:无对象
<form th:action="@{/destination}">
<input type="text" th:value="${service.firstName}" name="service.firstName"/>
<input type="text" th:value="${fleet.firstName}" name="fleet.firstName"/>
<button type="submit">Go</button>
</form>
选项2:
网路
<form th:action="@{/destination}" th:object="${myThymeleafFormObject}">
public class ThymeleafForm {
private String fleetFirstName;
private String serviceFirstName;
...
}
服务
public class MyService {
// To avoid complexity maps the ThymeleafForm in different JPA entities
// Logic and repository calls
}
储存库
public class FleetServiceRepositoryJPA {
//Database operations for fleet table
}
public class ServiceRepositoryJPA {
//Database operations for service table
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.