2

我的 spring mvc 应用程序中的控制器有问题。

我从数据库中获取所有实体并将它们放在我的 jsp 页面中的表中。

我正在添加一个实体,该功能运行良好,它添加了一个实体并刷新了页面。但是当我尝试刷新页面时出现问题......再次添加相同的实体,通常在每次刷新添加实体后再次执行 post 方法,最后我有许多相同的实体。

这是添加新实体的 post 方法:

@RequestMapping(value = "/adminpanel", method = RequestMethod.POST)
public ModelAndView addNewSoftware(@ModelAttribute VersionInformation versionInformation) 
{

    ModelAndView model = new ModelAndView("panel");

    persistanceDAO.insertVersionInformation(versionInformation);
    systemVersionsList.clear();
    systemVersionsList.addAll(persistanceDAO.getSystemVersions());
    model.addObject("systemVersionsList",systemVersionsList);
    model.addObject("versionInformation", new VersionInformation());

    return model;
}

这是我的 GET 方法:

@RequestMapping(value = "/adminpanel/add", method = RequestMethod.POST)
public ModelAndView addNewSoftware(@ModelAttribute VersionInformation versionInformation) 
{

    ModelAndView model = new ModelAndView("panel");

    persistanceDAO.insertVersionInformation(versionInformation);
    systemVersionsList.clear();
    systemVersionsList.addAll(persistanceDAO.getSystemVersions());
    model.addObject("systemVersionsList",systemVersionsList);
    model.addObject("versionInformation", new VersionInformation());

    return model;
}

另一个问题是,当我删除实体时,实体被删除,但网站没有刷新。这是我负责删除实体的 post 方法:

@RequestMapping(value = "/adminpanel/delete", method = RequestMethod.POST)
public ModelAndView deleteSoft(@ModelAttribute(value="currentId") int currentId, @ModelAttribute VersionInformation versionInformation) 
{

    ModelAndView model = new ModelAndView("panel");

    persistanceDAO.deleteSystemVersion(currentId);
    systemVersionsList.clear();
    systemVersionsList.addAll(persistanceDAO.getSystemVersions());
    model.addObject("systemVersionsList",systemVersionsList);
    model.addObject("versionInformation", new VersionInformation());


    return model;
}
4

4 回答 4

1

What about using redirect after updating the data.

"Another reason to perform a redirect before displaying the result is to eliminate the possibility of the user submitting the form data multiple times. In this scenario, the browser will first send an initial POST; it will then receive a response to redirect to a different URL; and finally the browser will perform a subsequent GET for the URL named in the redirect response. " quoted from Spring mvc doc

于 2013-07-26T13:37:00.110 回答
1

您可能正在查看Post/Redirect/Get pattern。如果您使用的是 Spring 3.1 及更高版本,则使用flash 属性很容易实现。

使用 Post/Redirect/Get Pattern 修改您的代码

@RequestMapping(value = "/adminpanel/add", method = RequestMethod.POST)
public String addNewSoftware(@ModelAttribute VersionInformation versionInformation, 
                                    final RedirectAttributes redirectAttributes){

    ModelAndView model = new ModelAndView("panel");

    persistanceDAO.insertVersionInformation(versionInformation);
    systemVersionsList.clear();
    systemVersionsList.addAll(persistanceDAO.getSystemVersions());
    redirectAttributes.addFlashAttribute("systemVersionsList",systemVersionsList);
    redirectAttributes.addFlashAttribute("versionInformation", new VersionInformation());

    return "redirect:/adminpanel/show-panel";
}

@RequestMapping(value = "/adminpanel/show-panel", method = RequestMethod.GET)
public ModelAndView showSoftwarePanel(@ModelAttribute("systemVersionsList") List<YouDidNotShowTheTypeOFSystemVersionList> systemVersionsList,
                                    @ModelAttribute("versionInformation") VersionInformation versionInformation){
    ModelAndView model = new ModelAndView("panel");
    model.addObject("systemVersionsList", systemVersionsList);
    model.addObject("versionInformation", versionInformation);

    return model;
}

这样做之后,您的页面现在可以避免多个表单提交问题。

于 2013-07-29T03:42:22.197 回答
0

GET方法只能用于幂等操作。对于删除和插入,您必须使用POST. 并且在非幂等操作成功后,您必须向用户发送重定向,因此如果他点击刷新,则不会伤害小猫。

于 2013-07-26T13:28:25.457 回答
0

认为问题在于,当您刷新页面时,您正在重新发送数据。如果是这样,您需要使用 Redirect-after-POST 模式。

尝试这个

@RequestMapping(value = "/adminpanel/add", method = RequestMethod.POST)
public ModelAndView addNewSoftware(@ModelAttribute VersionInformation versionInformation) 
{

    ModelAndView model = new ModelAndView("redirect:/admin/panel");

    // move these lines
    persistanceDAO.insertVersionInformation(versionInformation);
    systemVersionsList.clear();
    systemVersionsList.addAll(persistanceDAO.getSystemVersions());
    model.addObject("systemVersionsList",systemVersionsList);
    model.addObject("versionInformation", new VersionInformation());
    // end

    return model;

}

注意:您应该将注释行移动到 GET 方法中,否则,您的版本等将在重定向后出现在 URL 中

于 2013-07-26T19:29:49.157 回答