2013-07-26 182 views
2

我在我的彈簧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; 
} 
+0

您的獲取方法與您的發佈方法相同嗎?看起來像複製和粘貼錯誤。 –

+0

Okey,我將post方法更改爲「/ adminpanel/add」。瀏覽器轉到「adminpanel/add」,然後當我刷新它再次添加相同的實體。我如何強制它去「/ adminpanel」? –

回答

1

你可能正在尋找Post/Redirect/Get pattern。如果您使用的是Spring 3.1及更高版本,使用flash attribute可以輕鬆實現。

修改早報/重定向/你的代碼的獲取模式應用

@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; 
} 

這樣做之後,你的網頁是現在從多個表單提交的安全問題。

0

GET方法只能用於idempotent operations。對於刪除和插入,您必須使用POST。因此,如果他刷新,沒有任何小貓受到傷害。

1

更新數據後如何使用重定向?

「在顯示結果之前執行重定向的另一個原因是爲了消除用戶多次提交表單數據的可能性,在這種情況下,瀏覽器將首先發送一個初始POST;然後它會收到重定向到一個不同的URL;最後是瀏覽器將執行在重定向響應指定的URL隨後GET「quoted from Spring mvc doc

0

認爲的問題是,當你刷新頁面,你正在重新發送數據。如果是這樣,您需要使用重定向後模式。

試試這個

@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重定向後

相關問題