看了一下,@SessionAttributes和@ModelAttribute就是为了方便使用session.get/setAttribute和request.get/setAttribute而使用的类。
from:http://my.oschina.net/rouchongzi/blog/161871?fromerr=5UlNVC2r
Spring 2.0 定义了一个 org.springframework.ui.ModelMap 类,它作为通用的模型数据承载对象,传递数据供视图所用。我们可以在请求处理方法中声明一个 ModelMap 类型的入参,Spring 会将本次请求模型对象引用通过该入参传递进来,这样就可以在请求处理方法内部访问模型对象了。来看下面的例子:
使用 ModelMap 访问请示对应的隐含模型对象
1
2
3
4
5
6
7
8
9
10
|
@RequestMapping
(params =
"method=listBoardTopic"
)
public
String listBoardTopic(
@RequestParam
(
"id"
)
int
topicId,
User user,ModelMap model) {
bbtForumService.getBoardTopics(topicId);
System.out.println(
"topicId:"
+ topicId);
System.out.println(
"user:"
+ user);
//① 将user对象以currUser为键放入到model中
model.addAttribute(
"currUser"
,user);
return
"listTopic"
;
}
|
使模型对象的特定属性具有 Session 范围的作用域
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package
com.baobaotao.web;
…
import
org.springframework.ui.ModelMap;
import
org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@RequestMapping
(
"/bbtForum.do"
)
@SessionAttributes
(
"currUser"
)
//①将ModelMap中属性名为currUser的属性
//放到Session属性列表中,以便这个属性可以跨请求访问
public
class
BbtForumController {
…
@RequestMapping
(params =
"method=listBoardTopic"
)
public
String listBoardTopic(
@RequestParam
(
"id"
)
int
topicId, User user,
ModelMap model) {
bbtForumService.getBoardTopics(topicId);
System.out.println(
"topicId:"
+ topicId);
System.out.println(
"user:"
+ user);
model.addAttribute(
"currUser"
,user);
//②向ModelMap中添加一个属性
return
"listTopic"
;
}
}
|
使模型对象的特定属性具有 Session 范围的作用域
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package
com.baobaotao.web;
import
com.baobaotao.service.BbtForumService;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.ui.ModelMap;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.bind.annotation.SessionAttributes;
import
org.springframework.web.bind.annotation.ModelAttribute;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpSession;
@Controller
@RequestMapping
(
"/bbtForum.do"
)
@SessionAttributes
(
"currUser"
)
//①让ModelMap的currUser属性拥有session级作用域
public
class
BbtForumController {
@Autowired
private
BbtForumService bbtForumService;
@RequestMapping
(params =
"method=listBoardTopic"
)
public
String listBoardTopic(
@RequestParam
(
"id"
)
int
topicId, User user,
ModelMap model) {
bbtForumService.getBoardTopics(topicId);
System.out.println(
"topicId:"
+ topicId);
System.out.println(
"user:"
+ user);
model.addAttribute(
"currUser"
,user);
//②向ModelMap中添加一个属性
return
"listTopic"
;
}
@RequestMapping
(params =
"method=listAllBoard"
)
//③将ModelMap中的
public
String listAllBoard(
@ModelAttribute
(
"currUser"
) User user) {
//currUser属性绑定到user入参中。
bbtForumService.getAllBoard();
System.out.println(
"user:"
+user);
return
"listBoard"
;
}
}
|