无法从spring mvc中的控制器获取视图

时间:2012-09-23 09:06:21

标签: java spring java-ee spring-mvc

嗨,我在返回模型后无法获取视图页面,下面的视图是一些代码块

List<GridMappingVO> searchList= gridMappingFacade.validate(gridMappingVO);
if( users.equals(searchList.get(0).getUser()) 
    && passwrd.equals(searchList.get(0).getPass()) ) {
    System.out.println("validation succesfull");
    mv = new ModelAndView("searchResults");
    logger.info("Response is: " + mv);
    mv.addObject("searchList", searchList);
} else {
    List<String> list = new ArrayList<String>();
    list.add("Invalid");
    map.put("error_messages", list);
    mv.addAllObjects(map);
}
return mv;

STACK TRACE

08:51:31,713  INFO http-8080-1 controller.GridMappingController:45 - Response is: ModelAndView: reference to view with name 'searchResults'; model is null

08:51:31,728 DEBUG http-8080-1 support.DefaultListableBeanFactory:1367 - Invoking afterPropertiesSet() on bean with name 'searchResults'

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果您的条件不满意,它将转到else块。所以模型永远不会被实例化。 在if块

之外声明模型对象的实例化
mv = new ModelAndView("searchResults");
    List<GridMappingVO> searchList= gridMappingFacade.validate(gridMappingVO);
    if( users.equals(searchList.get(0).getUser()) 
    && passwrd.equals(searchList.get(0).getPass()) ) {
    System.out.println("validation succesfull"); 
    logger.info("Response is: " + mv);
    mv.addObject("searchList", searchList);
} else {
    List<String> list = new ArrayList<String>();
    list.add("Invalid");
    map.put("error_messages", list);
    mv.addAllObjects(map);
}
return mv;

你也可以这样做, 在方法的开头实例化一个hashmap 喜欢:

Map model = new HashMap();

并将对象添加到模型中。

model.put("searchList", searchList);

返回时创建一个新的ModelAndView对象

return new ModelAndView("jsp/searchResults", model)