我正在嘗試在單個表單上添加兩個物件,并遇到當它們具有相同名稱時連接欄位的問題。見下文
專案物體
@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 欄位作業得很好,因為它們是唯一的)。我添加了專案。和客戶。在這里查看另一個問題后,但這并沒有改變任何東西。我想避免將它們全部設為唯一欄位,因為我還想添加其他公司類(客戶端擴展公司)。這也適用于我想使用的其他欄位(城市/州等)。
我可以做些什么來確保兩個不同的地址沒有連接?提前致謝。
uj5u.com熱心網友回復:
如果要提交多個物件,則應創建一個包含所有物件的單頁表單物件并提交,而不是嘗試單獨提交每個物件。
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" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/464149.html
下一篇:引導導航連續折疊而不是垂直折疊
