使用 Thymeleaf/Spring Boot 添加具有相同名称字段的多个对象

Adding multiple objects with the same name fields using Thymeleaf/Spring Boot

我正在努力尝试在一个表单上添加两个对象,并且 运行 解决了当它们具有相同名称时连接字段的问题。见下文

项目实体

@Entity
@SequenceGenerator(name = "project_project_id_seq", allocationSize = 1, initialValue = 1)
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "project_project_id_seq")
    private Long projectId;

    private String projectName;
    private String address;

客户端实体

@Entity
@SequenceGenerator(name = "company_seq", sequenceName = "client_company_id_seq", allocationSize = 1, initialValue = 1)
public class Client extends Company {


    public Client(String companyName, String address, String city, String state, String zipcode) {
        super(companyName, address, city, state, zipcode);
    }

    public Client() {
    }
}

控制器

@Controller
@RequestMapping("/projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private ClientService clientService;

    @GetMapping("/addNew")
    public String addNewProject(Model model) {

        model.addAttribute("project", new Project());
        model.addAttribute("client", new Client());
        return "addNewProject";
    }

    @PostMapping("/addNew")
    public String checkProjectInfo(@ModelAttribute Project project, @ModelAttribute Client client, Model model) {

        model.addAttribute("project", project);
        model.addAttribute("client", client);

        clientService.addNew(client);

        project.setClient(client);

        projectService.addNew(project);
        return "projectAdded";
    }
}

相关 html 部分(我没有包含全部内容,但如果需要可以包含)

(Project portion)
<form action="#" th:action="@{/projects/addNew}" method="post">
    <div class="row px-5">
        <div class="card mb-2 ">
            <div class="card-header py-3">
                <h5 class="mb-0 text-danger">Project details</h5>
            </div>
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-md">
                        <input type="text" th:value="${project.projectName}" name="projectName" id="projectNameForm" class="form-control" placeholder="Project Name" />
                    </div>
                </div>

                <!-- Text input -->
                <div class="row mb-2">
                    <div class="col-md-9">
                        <input type="text" th:name="address" th:value="${project.address}" id="projectAddressForm" class="form-control" placeholder="Address" />
                    </div>

(Client portion)
<div class="row mb-2">
                            <div class="col-md">
                                <input type="text" th:field="${client.companyName}" id="clientNameForm" class="form-control" placeholder="Client Name" />
                            </div>
                        </div>
                        <!--Client address -->
                        <div class="row mb-2">
                            <div class="col-md">
                                <input type="text" th:name="address" th:value="${client.address}" id="clientAddressForm" class="form-control" placeholder="Address" />
                            </div>
                        </div>

我试过在同一个 div 中对它们中的每一个使用 th:object,尝试使用 th:field 而不是 th:value。问题是这会一直连接地址,因为它们具有相同的名称(projectName/clientName 字段工作得很好,因为它们是唯一的)。我添加了项目。和客户。在这里看了另一个问题之后,但这并没有改变任何东西。我想避免让它们都成为唯一字段,因为我也想添加其他公司 类(客户扩展公司)。这也适用于我想使用的其他字段(city/state 等)。

我可以做些什么来确保两个不同的地址不会串联在一起吗?提前致谢。

如果您想提交多个对象,您应该创建一个包含所有对象的页面表单对象并提交,而不是尝试单独提交每个对象。

class ProjectForm {
  private Project project;
  private Client client;
  
  // getters and setters
}

那么您的名字将是唯一的(应该类似于 project.address 等...)。

或者,如果您的项目实际上包含代码中的客户端 project.setClient(client);,您可以直接提交项目:

@GetMapping("/addNew")
public String addNewProject(Model model) {
    Project project;
    model.addAttribute("project", project = new Project());
    project.setClient(new Client());
    return "addNewProject";
}


<input type="text" th:value="${project.projectName}" name="projectName" id="projectNameForm" class="form-control" placeholder="Project Name" />
<input type="text" th:value="${project.client.address}" th:name="client.address"  id="clientAddressForm" class="form-control" placeholder="Address" />

最后,请注意,Thymeleaf 具有用于此的实用属性...表单实际上应该如下所示:

<form action="#" th:action="@{/projects/addNew}" th:object="${project}" method="post">
  <input type="text" th:field="*{projectName}" id="projectNameForm" class="form-control" placeholder="Project Name" />
  <input type="text" th:field="*{client.address}" id="clientAddressForm" class="form-control" placeholder="Address" />