这是关于两个 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;
}