大家好

请帮我。
我有应用程序和简单的Controller,它们从数据库中搜索数据,当在数据库中建立数据时,它也在浏览器中呈现它们,并且当页面上出现数据时,如果用户想要在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文件并在浏览器选项卡2中打开新的选项卡时,再次在选项卡2中查找并呈现一些不同的数据,然后在浏览器中返回到选项卡一并再次尝试从表1中保存与Excel文件相同的数据我从表2中获得数据(最新渲染),但是我想从选项卡1中获得旧数据
我在Servlets之前学习,并且对会话同步非常感兴趣。这是我的工作,在servlet中,可以通过以下方式实现:HttpSession session = request.getSession();
如何在Spring MVC中做到这一点?会解决我的问题吗?
请给我建议。

谢谢大家
一切顺利。

最佳答案

您可以通过添加参数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应用程序将无法扩展。

10-07 19:23
查看更多