简体   繁体   English

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

[英]Spring MVC/Thymeleaf - How to insert to two different tables using one input field?

I have an input field for first name: 我有一个名字输入字段:

<input th:field="*{fleet.firstName}" class="signup1" type="text" id="fname" name="fname" autofocus="" required=""/>

I have two tables/objects 'fleet' and 'service.' 我有两个表/对象“舰队”和“服务”。 How can I do that if thymeleaf only allows me to use one object per input field? 如果百里香仅允许我在每个输入字段中使用一个对象,该怎么办?

I tried two input th:field, but it didn't work. 我尝试了两次输入th:field,但是没有用。 Like: 喜欢:

<input th:field="*{fleet.firstName}" th:field="*{service.firstName}" class="signup1" type="text" id="fname" name="fname" autofocus="" required=""/>

I think this is not possible. 我认为这是不可能的。 You could create a DTO object for Thymeleaf with the union of the fields that you are trying to see in the web side. 您可以为Thymeleaf创建一个DTO对象,并结合您尝试在Web端看到的字段。 Then, separate this into database tables in you Service tier. 然后,将其分成服务层中的数据库表。

Something like: 就像是:

Option 1: WITHOUT th:object 选项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>

Option 2: 选项2:

web 网路

<form th:action="@{/destination}" th:object="${myThymeleafFormObject}">

public class ThymeleafForm {

    private String fleetFirstName;
    private String serviceFirstName;

    ...

}

Service 服务

public class MyService {

    // To avoid complexity maps the ThymeleafForm in different JPA entities 
    // Logic and repository calls
}

Repositories 储存库

public class FleetServiceRepositoryJPA {    
    //Database operations for fleet table
}

public class ServiceRepositoryJPA { 
    //Database operations for service table
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Thymeleaf和Spring MVC以一种形式添加属于两个具有不同属性名称的两个不同模型的对象 - Adding two objects that belong to two different models with similar attribute names in one form using Thymeleaf and Spring MVC Thymeleaf + Spring MVC - Thymeleaf 使用复选框输入时模板解析错误 - Thymeleaf + Spring MVC - Thymeleaf template parsing error on using checkbox input 如何使用 2 个不同的表在 thymeleaf 中循环? - How to loop in thymeleaf using 2 different tables? 使用CrudRepository和Thymeleaf的Spring MVC(文本字段和按钮) - Spring MVC (text field and button) using CrudRepository and Thymeleaf 如何使用Java和MySQL在一个语句中插入两个不同的表? - How to insert into two different tables in one statement with Java and MySQL? 如何在Spring MVC中的一个jsp中从两个表中插入数据 - How to insert data from two table in one jsp in spring mvc an Spring MVC一个输入字段表单 - Spring MVC one input field form 如何使用 Spring MVC 和 hibernate 连接/绑定两个表 - How to connect/bind two tables using Spring MVC and hibernate 如何单击按钮Spring / Java / Thymeleaf来添加新的输入字段 - How to add a new input field at click of button Spring/Java/Thymeleaf Spring MVC 验证和 Thymeleaf - 验证 Integer 字段 - Spring MVC validation and Thymeleaf - validating Integer field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM