在Spring MVC应用程序中检索会话数据

时间:2013-10-13 05:16:03

标签: spring-mvc

我继承了一个Spring MVC应用程序。我对.NET 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被用作参数和结果,所以我不能很好地使它成为一个会话范围的bean。我想我能做的是:

  1. 将此添加到SearchController:@SessionAttributes(“searchPojo”)
  2. 修改@ModelAttribute - > @ModelAttribute( “searchPojo”)
  3. 然而,我不知道该怎么办才能从CrudController访问searchPojo,因为我想在其上设置一个message属性,以便在搜索结果页面上显示。

    我看到将会话传递给控制器​​动作的示例不使用属性,所以我只是不确定它应该是什么样的。

    提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您使用的是大于3.1.2.RELEASE的Spring版本,则只需将@SessionAttributes("searchPojo")添加到CrudController的顶部,然后指定{{1} }作为ModelAttibute("searchPojo")方法的参数。

modifyRecord

Spring应该在会话中查找@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内的searchPojo设置邮件属性。

这里有一个使用modifyRecord在控制器之间共享数据的示例:Spring 3.0 set and get session attribute

答案 1 :(得分:0)

也许您可以尝试从请求中获取会话并将pojo设置为会话属性。

相关问题