【问题标题】:Session Synchronization in Spring MVCSpring MVC 中的会话同步
【发布时间】:2013-10-17 15:58:19
【问题描述】:

大家好。

请帮助我。 我有应用程序和简单的控制器,它们从数据库中搜索数据,当在数据库中创建数据时,它也会在浏览器中呈现它们,当数据出现在页面上时,如果用户想要在 Excel 文件中呈现它,还会出现Render in EXCEL 按钮。这是控制器:

@Scope("session")
@Controller
public class SearchController {

    //Apache log4j logger for this controller to bring info to console of executing inside of this controller
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(SearchController.class);


        @Autowired
        private EducationWebServiceInterface educationWebService;

        List<FormDate> listForm = null;


        @ModelAttribute("listOfDates") 
        public List<Date> prepareModelDate() {      
            List<Date> listOfDates = educationWebService.fetchAllDatesService();
            return listOfDates;     
        }


        @ModelAttribute("listOfNames")
        public List<String> prepareModelNames() {       
            List<String> listOfNames = educationWebService.fetchAllInstitutionNamesService();
            return listOfNames;     
        }


        @ModelAttribute("listOfTypes")
        public List<String> prepareModelTypes() {       
            List<String> listOfTypes = educationWebService.fetchAllInstitutionTypesService();
            return listOfTypes;     
        }


        @RequestMapping(value="/search", method=RequestMethod.GET)
        public String search(FormBackingObjectSearch fbos, Model model) {   
                model.addAttribute("fbosAttributes", fbos);
            return "search";        
        }   


        @RequestMapping(value="/result", method=RequestMethod.GET)
        public String resultHTML(@RequestParam String particularDate,
                                 @RequestParam String nameOfInstitution,
                                 @RequestParam String typeOfInstitution,
                                 @ModelAttribute("fbosAttributes") @Validated FormBackingObjectSearch fbos,  
                                                                              BindingResult bindingResult,
                                                                              Model model) throws Exception {

            ValidatorSearch validatorSearch = new ValidatorSearch();
                validatorSearch.validate(fbos, bindingResult);

            if(bindingResult.hasErrors()) {         
                return "search";            
                }


            listForm = new ArrayList<FormDate>();


            //Case 1:
            if(!fbos.getParticularDate().equals("") && !fbos.getNameOfInstitution().equals("") && fbos.getTypeOfInstitution().equals("")) {         
                listForm = educationWebService.fetchByDateAndNameService(DateChangerUtils.dateConvertation(fbos.getParticularDate()), fbos.getNameOfInstitution());
                model.addAttribute("findAttributes", listForm);
            //Case 2:
            } else if(!fbos.getParticularDate().equals("") && fbos.getNameOfInstitution().equals("") && !fbos.getTypeOfInstitution().equals("")) {                      
                listForm = educationWebService.fetchByDateAndTypeService(DateChangerUtils.dateConvertation(fbos.getParticularDate()), fbos.getTypeOfInstitution());
                model.addAttribute("findAttributes", listForm);
            //Case 3:
            } else if(!fbos.getParticularDate().equals("") && fbos.getNameOfInstitution().equals("") && fbos.getTypeOfInstitution().equals("")) {           
                listForm =  educationWebService.fetchByDateService(DateChangerUtils.dateConvertation(fbos.getParticularDate()));
                model.addAttribute("findAttributes", listForm);
            //Case 4:
            } else {        
                throw new Exception("Exception occurs because it's not correspond to any case in controller");
            }       
            return "search";
        }


        @RequestMapping(value="/result.xls", method=RequestMethod.GET)
        public String resultXLS(Model model) throws NullPointerException {

                    if(listForm == null || listForm.isEmpty() == true) {                
                        throw new NullPointerException("Can not create Excel file because no data to create from");             
                    } else {
                    model.addAttribute("findAttributesXls", listForm);
                    }               
                return "xlspage";
        } //End of the resultXLS(..) method
} //End of the class

现在让我们在浏览器中使用标签: 我的问题是,当我将浏览器标签一中的渲染数据保存为 Excel 文件并在浏览器标签二中打开新标签时,再次在标签二中找到并渲染一些不同的数据,然后返回浏览器中的标签一,然后再次尝试将表一中的相同数据保存为 Excel 文件我从表二中获取数据(我呈现的最新数据),但我想从标签一中获取旧数据 我在 Servlets 之前学习并且对会话同步非常感兴趣。在我的情况下,这是我的作品在 servlet 中,可以通过以下方式访问它:HttpSession session = request.getSession(); 我怎么能在 Spring MVC 中做到这一点???它会解决我的问题吗? 请给我建议。

谢谢大家。 祝你一切顺利。

【问题讨论】:

  • 你为什么要抛出 NullPointerException ?
  • 在我的 resultXLS(...) 方法中?认为我必须检查我的模型数据是否为空,如果发生,则使用 NullpointerException 呈现自定义消息。不对吗??
  • NullPointerException 不得处理或不得声明为方法签名的一部分(抛出)。如果您收到 NullPointerException,则意味着您的代码中存在一些错误。
  • 哦。我不知道。先生,谢谢您的精彩回答。请您指导我有关您发现的抛出问题的文档。

标签: java spring servlets spring-mvc


【解决方案1】:

您可以通过添加参数HttpSession session

来访问控制器方法中的会话
 @RequestMapping(value="/result.xls", method=RequestMethod.GET)
    public String resultXLS(HttpSession session, Model model) throws NullPointerException {
    Object myObj = session.getAttribute("myAttr");
    ....
 }

另一种选择是在控制器方法中使用 HttpServletRequest 类型的参数

@RequestMapping(value="/result.xls", method=RequestMethod.GET)
    public String resultXLS(HttpServletRequest request, Model model) throws NullPointerException {
    Object myObj = request.getSession(false).getAttribute("myAttr");
    ....
 }

但是,同步会话并不能解决您的问题。会话同步最适合大量并行请求不断出现并修改存储在会话中的共享数据的情况。你说的不是这种情况。

您想要的是基于选项卡的状态,这是您无法获得任何现成解决方案的东西,这也不是一个好习惯。这将使您的会话变得非常繁重,并且您的 Web 应用程序将无法扩展。

【讨论】:

    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2011-11-01
    • 2013-12-01
    • 1970-01-01
    • 2018-07-09
    • 2014-11-10
    • 2020-12-16
    相关资源
    最近更新 更多