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

发布于 2025-01-23 08:49:30 字数 3729 浏览 0 评论 0原文

我正在尝试在单个表单上添加两个对象并陷入问题,而该字段具有相同的名称时会串联。请参阅下面的

项目实体

@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;

client entity

@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:对象,尝试使用th:field而不是th:价值。问题在于,这是因为它们具有相同的名称(projectName/clientname字段都可以正常工作,因为它们唯一的功能很好)。我添加了项目。和客户。在这里查看了另一个问题之后,但这并没有改变任何问题。我想避免使它们成为所有独特的领域,因为我也想添加其他公司课程(客户端扩展公司)。这也适用于我想使用的其他字段(城市/州等)。

我可以做些什么来确保两个不同的地址没有连接?提前致谢。

I'm working on trying to add two objects on a single form and running into issues with the field being concatenated when they have the same name. See below

Project Entity

@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;

Client Entity

@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

@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";
    }
}

Relevant html portions (I didn't include the whole thing but can if needed)

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

I've tried using th:object for each of them within the same div, tried using th:field instead of th:value. The problem is this keeps concatenating the address because they have the same name (the projectName/clientName fields work just fine as they're unique). I added the project. and client. after looking at another question here but that didn't change anything. I want to avoid making them all unique fields because I want to add other Company classes as well(Client extends Company). This also applies for other fields I want to use (city/state etc).

Is there something I can do to make sure the two different addresses are not concatenated? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情何以堪。 2025-01-30 08:49:30

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

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" />

If you want to submit multiple objects, you should create a single page form object that contains all your objects and submit than, instead of trying to submit each object individually.

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

Then your names will be unique (should be something like project.address etc...).

Alternatively, if your projects actually contains a client as in your code project.setClient(client);, you can just submit the project:

@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" />

Finally, just as a note, Thymeleaf has utility properties for this... the form should actually look like this:

<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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文