2013-10-17 30 views
1

大家好日子。Spring MVC中的會話同步

請幫幫我。 我有應用程序和簡單的控制器,從數據庫中搜索數據,當數據建立在數據庫中時,它也在瀏覽器中呈現它們,當數據出現在頁面上時,如果用戶想要在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表文件保存相同的數據作爲Excel文件我從表二(最新我渲染)的數據,但我想獲得我的舊數據從標籤之一 我在學習Servlets之前對真正感興趣的會話同步。這是我的作品在我的情況下在servlets中,它可以達到:HttpSession session = request.getSession(); 我怎麼能在Spring MVC中做到這一點?它會解決我的問題嗎? 請給我建議。

謝謝你們。 一切順利。

+0

爲什麼你有decalred throws NullPointerException? –

+0

在我的resultXLS(...)方法??認爲我必須檢查我的模型數據爲空,並在發生時使用NullpointerException呈現自定義消息。這不對嗎? – Vad

+0

不能處理NullPointerException,也不能將其聲明爲方法簽名(引發)的一部分。如果你得到NullPointerException,那意味着你的代碼有一些錯誤。 –

回答

0

您可以通過添加參數在你的控制器的方法訪問會話 HttpSession的會議

@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應用程序不會擴展。