2013-10-13 39 views
0

我繼承了Spring MVC應用程序。我熟悉.NET MVC,我想我會非常不同設計的這件事情,但我有什麼工作,這是我想做什麼:在Spring MVC應用程序中檢索會話數據

當前設計:

SearchController - 提交和呈現搜索結果。每個單獨的搜索結果都有一個添加/編輯/刪除選項,用於向不同的控制器提交請求。

@RequestMapping(valur={"/searchResult.do"}...)   
public ModelAndView searchResult(@ModelAttribute GlobalPojo pojo) { 
    //here I'd like to store the pojo in session so that the search results 
    //can be re-rendered after an Add/Edit/Delete action 
} 

CrudController

@RequestMapping(value = { "/modifyAction.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }) 
public ModelAndView modifyRecord(@RequestParam("id") String uid) { 
    //here I'd like to return to the results page after completing an action 
} 

這GlobalPojo作爲參數,並導致所有的地方,所以我不能很好地使一個會話範圍豆。我想我可以做的是:

  1. 一下添加到SearchController:@SessionAttributes( 「searchPojo」)
  2. 修改@ModelAttribute - > @ModelAttribute( 「searchPojo」)

但是,我不知道該怎麼做,然後從CrudController訪問searchPojo,因爲我想在其上設置一個消息屬性以顯示在搜索結果頁面上。

我看到的將會話傳遞給控制器​​動作的例子不使用屬性,所以我只是不確定應該看起來像什麼。

在此先感謝。

回答

1

只要你正在使用的版本春天大於3.1.2.RELEASE這應該只是增加@SessionAttributes("searchPojo")CrudController頂部,然後指定ModelAttibute("searchPojo")上的modifyRecord方法的參數的情況下。然後

@Controller 
@SessionAttributes("searchPojo") 
public class CrudController { 
    ... 

    @RequestMapping(value = { "/modifyAction.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }) 
    public ModelAndView modifyRecord(@RequestParam("id") String uid, @ModelAttribute("searchPojo") SearchPojo pojo) { 
     //here I'd like to return to the results page after completing an action 
    } 
} 

春季應該尋找searchPojo會話,並通過它傳遞到modifyRecord方法。然後,您應該能夠在modifyRecord內的searchPojo上設置消息屬性。

有使用SessionAttributes這裏控制器之間的數據共享的一個例子:Spring 3.0 set and get session attribute

+0

感謝 - 我們不幸的是在3.0.5或類似的東西。但是,如果這不起作用,我會嘗試鏈接中列出的版本。 – sydneyos

+0

@SessionAttribute方法無效(同樣,我們在早期版本中),但所提供的鏈接確實有效。也就是說,將HttpSession傳遞給控制器​​所涉及的動作,並在搜索動作中執行session.setAttribute,並在CRUD控制器動作中執行session.getAttribute。 – sydneyos

0

也許你可以嘗試從請求獲取會話並將pojo設置爲會話屬性。

+0

正確的,但後來它成爲訪問Request對象 – sydneyos

+0

你試過把它作爲一個參數的問題?像'public ModelAndView searchResult(@ModelAttribute GlobalPojo pojo,HttpServletRequest req){HttpSession session = req.getSession();}' – hsluo

+0

「我看到的將會話傳遞給控制器​​操作的示例不使用屬性,所以我只是不確定那應該是什麼樣子。「 - 所以不,我不確定我是否可以這樣做,或者是否需要按照特定的順序,正如這些例子似乎表明的那樣。 – sydneyos

相關問題