Spring MVC/Thymeleaf - 如何使用一个输入字段插入到两个不同的表中



我有一个名字输入字段:

<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:不带 th:对象

<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
}

最新更新