简体   繁体   中英

Session Synchronization in Spring MVC

Good day everybody.

Please help me. I have application and simple Controller which search data from database and when data founded in database it render them in browser also when data appears on page there is also Render in EXCEL button appears if user wants render it in Excel file. Here is controller:

@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

Now let's play with tabs in browser: My problem is that when I save rendered data in browser tab one as Excel file once and open new tab in browser tab two, find and render some different data in tab two again and after come back to tab one in my browser and again trying to save same data from table one as Excel file I get data from table two(latest I render), but I want to get my old data from tab one I learn before Servlets and really interested of session synchronization. It my works in my case In servlets it just can be reached by: HttpSession session = request.getSession(); How can I do this in Spring MVC??? And does it will solve my problem?? Please give me advice.

Thank you people. With all the best.

You can access the session in your controller method by adding a parameter HttpSession session

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

Another alternative is to have a parameter of type HttpServletRequest in the controller method

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

But, Synchronizing the session will not solve your problem. Session synchronization is best suited for the situations where a lot of parallel requests keep coming and modifying the shared data which is stored in session. This is not the case you are talking about.

What you want is something like tab based states which is something you would not get any ready made solution, neither it's a good practice to go for. It will make your session very heavier and your web-application will not scale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM