【问题标题】:Spring redirectAttributes not transferredSpring redirectAttributes 未传输
【发布时间】: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


    【解决方案1】:

    在您的 savePerson 方法中,您添加的是 attributes.addFlashAttribute() 而不是 model.addAttribute(),就像您在 showAddKontoPage 方法中一样。

    意思是,对于要通过重定向从控制器传递到控制器的属性,您需要通过model.addAttribute() 方法添加该属性。

    您可以在控制器方法中添加一个新属性以接受用户 id 属性:

    @RequestMapping(value = {"/addKonto"}, method = RequestMethod.GET)
    public String showAddKontoPage(Model model,
                                   @RequestParam(required = false) String id)
    
    @RequestMapping(value = {"/addKonto"}, method = RequestMethod.POST)
    public String saveKonto(Model model,
                            @ModelAttribute("kontoForm") KontoForm kontoForm,
                            @RequestParam(required = false) String id)
    

    Flash Messages 用于通过标签向最终用户显示消息。

    附注:

    Spring 拥有Validate your Forms 的基础架构。当然,当您准备好使用该基础架构时,将来使用该基础架构会更好。

    【讨论】:

    • 如果我添加这个 model.addAttribute("id", newPerson.getId());保存人,然后 showAddPersonPage 不接受 id。问题是在 saveKonto 中模型只有 kontoForm 而没有用户 ID。
    • size = 3 表示模型有kontoForm、validator和id
    • 如果您的表单中没有用户 ID,那么您需要通过控制器方法将值作为参数传递,或者您需要将用户 ID 添加到您的表单.我建议通过方法属性传递用户 ID。
    • 我更新了答案以显示如何将参数属性传递给您的控制器方法。
    • 那么 id 为空
    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多