使用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;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要提交多个对象,则应创建一个包含所有对象并提交的单页表单对象,而不是尝试单独提交每个对象。
那么您的名字将是唯一的(应该像
project.address
等...)。另外,如果您的项目实际上包含一个客户端
project.setclient(client);
,则可以提交项目:最后,如注释,Thymeleaf为此具有实用属性。表格实际上应该像这样:
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.
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:Finally, just as a note, Thymeleaf has utility properties for this... the form should actually look like this: