0

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

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;

堆栈跟踪

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'

谁能帮我看看这有什么问题?

4

1 回答 1

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;

您也可以这样做,在方法的开头实例化一个哈希图,例如:

Map model = new HashMap();

并将对象添加到模型中。

model.put("searchList", searchList);

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

return new ModelAndView("jsp/searchResults", model)
于 2012-09-23T14:26:37.387 回答