2
這是關於兩個Spring 3控制器之間共享會話的問題。如何從另一個控制器的會話屬性中刪除?
一個控制器處理實體的顯示。它將數據庫選擇結果緩存在@SessionAttributes中。另一個控制器處理實體的添加和刪除。每當第二個控制器添加或刪除一個實體時,挑戰就是刪除第一個控制器的會話緩存。
指數控制器
@Controller
@RequestMapping(value="/fileIndex")
@SessionAttributes(value={IndexController.INDEX_ITEM_LIST})
public class FileIndexController extends IndexController {
if(!model.containsAttribute(IndexController.INDEX_ITEM_LIST)) {
model.addAttribute(IndexController.INDEX_ITEM_LIST, getFileList(screenObject)); // sql select
}
screenObject.setPageItemList((List<?>) model.asMap().get(IndexController.INDEX_ITEM_LIST), pageNumber); // page of entities (subset of cached sql select)
動作控制器 - 刪除方法
@RequestMapping(method=RequestMethod.POST, params=ACTION_DELETE)
public ModelAndView delete(@ModelAttribute("screenObject") FileHeaderEditScreenObject screenObject, BindingResult bindingResult, Model model, Locale locale) {
try {
fileService.deleteFile(screenObject.getFileId());
} catch(Throwable t) {
screenObject.addError(t.getMessage());
}
ModelAndView modelAndView=new ModelAndView();
if(screenObject.getErrorCount()>0) {
initializeScreenObject(screenObject.getFileId(), screenObject, locale, false);
modelAndView.setViewName(WebView.FILE_HEADER_EDIT_PAGE.getViewName());
} else {
modelAndView.setViewName("redirect:/fileIndex");
}
model.asMap().remove(IndexController.INDEX_ITEM_LIST); // this does not work
return modelAndView;
}