【发布时间】:2021-01-26 15:31:16
【问题描述】:
我想将 id 参数从一种形式(控制器)传递到另一种形式。 在这里,我为我的模型添加了一个 id 并传递它。
@Controller
public class PersonController
@RequestMapping(value = {"/addPerson"}, method = RequestMethod.POST)
public String savePerson(Model model, //
@ModelAttribute("personForm") PersonForm personForm, RedirectAttributes attributes) {//, @ModelAttribute("person") Person newPerson) {
String firstName = personForm.getFirstName();
String lastName = personForm.getLastName();
double money = personForm.getMoney();
if (firstName != null && firstName.length() > 0
&& lastName != null && lastName.length() > 0) {
Person newPerson = new Person(firstName, lastName, money);
personService.save(newPerson);
attributes.addFlashAttribute("id", newPerson.getId());//
return "redirect:/addKonto";
}
model.addAttribute("errorMessage", errorMessage);
return "addPerson";
}
我正在将数据传递给另一个表单,这里的 id 被正确传递。大小 = 3
@RequestMapping(value = {"/addKonto"}, method = RequestMethod.GET)
public String showAddKontoPage(Model model, @ModelAttribute("id") int id) {
KontoForm kontoForm = new KontoForm();
model.addAttribute("kontoForm", kontoForm);
model.addAttribute("id", id);
return "addKonto";
}
当我尝试进一步传递我的模型时,id 参数会丢失。大小 = 2
@RequestMapping(value = {"/addKonto"}, method = RequestMethod.POST)
public String saveKonto(Model model,
@ModelAttribute("kontoForm") KontoForm kontoForm) {
String kontoName = kontoForm.getKontoName();
double moneyInKonto = kontoForm.getMoneyInKonto();
model.getAttribute("id"); // hier is null
if (kontoName != null && kontoName.length() > 0) {
Konto newKonto = new Konto(kontoName, moneyInKonto);
kontoService.save(newKonto);
return "redirect:/personList";
}
如何获取id参数?
【问题讨论】:
标签: java spring-boot spring-mvc thymeleaf