Spring属性未添加到模型中

时间:2015-11-13 20:36:18

标签: spring spring-mvc model-view-controller model controller

我想为模型添加一些属性,但由于某些原因,其中一个属性没有被添加到特定请求处理程序的模型中。

这两个属性为numberDeletedusersDeleted

由于某种原因,

usersDeleted未添加到第三个请求处理程序的模型中。

以下是代码:(注意:为简洁起见,已删除了一些代码行。仅显示相关代码。)

首先,我删除了一些选定的用户:

@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public String delete(
            @ModelAttribute("credentials") Credentials credentials,
            @ModelAttribute("deleteUsersForm") DeleteUsersForm form,
            @ModelAttribute("searchCriteria") SearchForm searchForm,
            Model model, RedirectAttributes redirect) {

        String[] usersDeleted = form.getCheckedUsers();
        if (usersDeleted != null) {
            for (int i = 0; i < usersDeleted.length; i++) {
                System.out.println("user (delete): " + usersDeleted[i]);
            }
        }

        redirect.addFlashAttribute("usersDeleted", usersDeleted);
        redirect.addFlashAttribute("numberDeleted", numberDeleted);
        redirect.addFlashAttribute("searchForm", searchForm);

        return "redirect:" + searchForm.getSelectedOption();
    }

从数据库中删除所选用户后,我进行REST API调用以获取当前现有用户并将其添加到模型中,以及删除中的numberDeletedusersDeleted功能如上:

@RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    public String getAllUsers(@ModelAttribute Credentials credentials,
            RedirectAttributes redirect, Model model) {

        SearchForm searchForm = new SearchForm("/getAllUsers", "allUsers");

        redirect.addFlashAttribute("users", users);
        redirect.addFlashAttribute("searchCriteria", searchForm);

        /* If users deleted: */
        Integer numberDeleted = (Integer) model.asMap().get("numberDeleted");
        String[] usersDeleted = (String[]) model.asMap().get("usersDeleted");
        redirect.addFlashAttribute("numberDeleted", numberDeleted);
        redirect.addFlashAttribute("usersDeleted,", usersDeleted);

        if (usersDeleted != null) {
            for (int i = 0; i < usersDeleted.length; i++) {
                System.out.println("user: (getAllUsers) " + usersDeleted[i]);
            }
        }

        return "redirect:/adminHome";
    }

此时usersDeletednumberDeleted不为空,我可以打印出它们的值。

然后我调用请求列出用户:

@RequestMapping(value = "/adminHome", method = RequestMethod.GET)
    public String getAdminHomePage(Model model) {


        SearchForm searchCriteria = (SearchForm) model.asMap().get(
                "searchCriteria");

        /* If users were deleted: */
        Integer numberDeleted = (Integer) model.asMap().get("numberDeleted");
        String[] usersDeleted = (String[]) model.asMap().get("usersDeleted");
        model.addAttribute("numberDeleted", numberDeleted);
        model.addAttribute("usersDeleted,", usersDeleted);

        if (usersDeleted != null) {
            for (int i = 0; i < usersDeleted.length; i++) {
                System.out.println("user: (adminhome) " + usersDeleted[i]);
            }
        }
         .....
        return "admin_home";
    }

但是,此时usersDeleted为空(因为没有打印出来),但numberDeleted不为空。

我真的不明白发生了什么。怎么会是null?我明确地将它添加到模型中。

编辑1: 目前,我唯一的解决方法是将usersDeleted添加到@SessionAttributes

但我不应该诉诸于此。

1 个答案:

答案 0 :(得分:1)

根据@Kingamere的评论,我发帖了。模型键上有拼写错误。

model.addAttribute("usersDeleted,", usersDeleted);

在usersDeleted结尾处有一个额外的逗号。删除该逗号后,它可以完美地用于Kingmere。

model.addAttribute("usersDeleted", usersDeleted);