V2EX › Java
Spring MVC 中 ModelAndView.addObject 后的属性值为什么不在 request 域中?
By flight2006 at 2015 年 6 月 11 日 · 10495 次点击Controller的代码片段:
================================================
@RequestMapping(value = "/index",method = RequestMethod.GET)
public ModelAndView listAllBoards(){
ModelAndView view = new ModelAndView();
List<Board> boards = forumService.getAllBoards();
view.addObject("boards",boards);
view.setViewName( "/listAllBoards");
return view;
}
==========================================
我断点调试这个view里面是有boards属性的,说明和Service层无关。
单元测试代码片段:
===========================================
@Test
public void listAllBoards() throws Exception {
request.setRequestURI("/index");
request.setMethod("GET");
//调用控制器
ModelAndView view = handlerAdapter.handle(request, response, controller);
List<Board> boards = (List<Board>) request.getAttribute("boards"); //★
//验证结果boards
assertNotNull(view);
assertEquals("/listAllBoards",view.getViewName());
assertNotNull(boards);
assertThat(boards.size(),greaterThan(1));
=====================================
★处得到的boards是null值,断点调试request里面也是没有boards属性的
当我把★处的代码换成下面这行
List<Board> boards = (List<Board>) view.getModelMap().get("boards");
这时候boards是能得到的。不是说ModelAndView.addObject()相当于往request域里存这个值吗?那为什么request里面会没有这个值?初学Spring MVC,Any help would mean a lot
================================================
@RequestMapping(value = "/index",method = RequestMethod.GET)
public ModelAndView listAllBoards(){
ModelAndView view = new ModelAndView();
List<Board> boards = forumService.getAllBoards();
view.addObject("boards",boards);
view.setViewName( "/listAllBoards");
return view;
}
==========================================
我断点调试这个view里面是有boards属性的,说明和Service层无关。
单元测试代码片段:
===========================================
@Test
public void listAllBoards() throws Exception {
request.setRequestURI("/index");
request.setMethod("GET");
//调用控制器
ModelAndView view = handlerAdapter.handle(request, response, controller);
List<Board> boards = (List<Board>) request.getAttribute("boards"); //★
//验证结果boards
assertNotNull(view);
assertEquals("/listAllBoards",view.getViewName());
assertNotNull(boards);
assertThat(boards.size(),greaterThan(1));
=====================================
★处得到的boards是null值,断点调试request里面也是没有boards属性的
当我把★处的代码换成下面这行
List<Board> boards = (List<Board>) view.getModelMap().get("boards");
这时候boards是能得到的。不是说ModelAndView.addObject()相当于往request域里存这个值吗?那为什么request里面会没有这个值?初学Spring MVC,Any help would mean a lot
6 条回复 • 2015-08-30 21:36:39 +08:00
|
1
Troevil 2015 年 6 月 11 日
view addObject 作用域应该是Page 不是request
|
|
2
flight2006 OP @Troevil 从查到的资料看,应该不是Page,是request,Page域离开那个页面就失效了,无法传给controller
|
|
3
Troevil 2015 年 6 月 11 日
@flight2006 传给controller?不太懂你的意思, spring的流程不是 拦截请求,丢给controller处理完再返回view? 而你addObject 仅在那个返回的view里面生效,那作用域不就是page吗? 文档我倒是没看过。因为你是view add的Object 不是 request 或者 session add的
|
|
4
flight2006 OP @Troevil Page作用域只是在当前的JSP中有效吗,controller拦截请求后不就离开的请求的页面么,这个view是跟着controller拦截到的request走的,返回的view其实是可以redirect到另外一个页面的
|
|
5
cloudhunter 2015 年 6 月 11 日 你往 ModelAndView 塞东西之后,要到很后 JSP 渲染的时候,ModelAndView 才往 request.setAttribute() 里塞东西。
所以 ModelAndView view = new ModelAndView(); view.addObject("boards", boards); request.getAttribute("boards"); // 这里会返回null |
|
6
lrj2u 2015 年 8 月 30 日
我觉得你是导错包了,我也因为这个问题搜索了半天。后来找到问题了。
import org.springframework.web.servlet.ModelAndView (正确应该引入这个) improt org.springframework.web.portlet.ModelAndView (这个是错误的) |