2017-07-31 132 views
0

在Spring應用程序中,我需要保留用戶值直到我不刪除或銷燬。春季更改了會話值

根據它,我已經使用了的HttpSession在Controller如下

@Controller 
public class MyController { 

@RequestMapping(value = { "/search" }, method = RequestMethod.POST) //this hander called once 
public String search(SearchVo aSearchVo, BindingResult result, 
     ModelMap model,HttpSession httpsession) { 
    if (result.hasErrors()) { 
     model.addAttribute("searches", new SearchVo()); 
     return "home"; 
    }  
    httpSession.setAttribute("searchstring", aSearchVo.getSearchString()); 

    return "caseResult"; 
} 



@SuppressWarnings("unchecked") 
@RequestMapping(value = { "/filtersearch" }, method = RequestMethod.POST) //This handler call again and again 
public String filterSearch(@ModelAttribute("filter") FilterVo fvo,ModelMap model , HttpSession httpSession){ 

     String searchKeyWorld=httpSession.getAttribute("searchstring"); 
      System.out.println(searchKeyWorld); 

      searchKeyWorld+=fvo.getFilterWorld(); 

    return "caseResult"; 
    } 
} 

但在會話變量,該值被作爲在最後一個過濾器自動地改變;因爲我沒有在filtersearch中設置任何會話變量Handler

+0

我還沒有正確理解你的問題 –

回答

0

您需要使用@SessionAttributes將變量放入http會話中。 這是一個級別的註釋。

@Controller 
@SessionAttributes("searches") 
public class MyController{ 

@RequestMapping(value = { "/search" }, method = RequestMethod.POST) //this hander called once 
public String search(SearchVo aSearchVo, BindingResult result, 
     ModelMap model,WebRequest webRequest) { 
    if (result.hasErrors()) { 
     model.addAttribute("searches", new SearchVo()); 
     return "home"; 
    }  
    model.addAttribute("searches", new SearchVo()); 
    //For removing anything from session 
    //webRequest.removeAttribute("searches", WebRequest.SCOPE_SESSION); 
    return "caseResult"; 
} 

} 
+0

我已經試過了!還是一樣的輸出 –

+0

您是否嘗試讀取(SearchVo)webRequest.getAttribute(「Session variable name」,WebRequest.SCOPE_SESSION)等變量;並使用Model來代替ModelMap – Yogi