最近学习spring mvc 时碰到找不到页面的情况,看提示信息请求页面是请求的映射的页面,但是我根本没有返回任何页面,是Void类型的,查了一下才发现这是spring规定的格式,Void类型的返回值就是请求地址对应的页面:
1.对于ModelAndView构造函数可以指定返回页面的名称,也可以通过setViewName方法来设置所需要跳转的页面
@RequestMapping(method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView("/user/index");
modelAndView.addObject("xxx", "xxx");
return modelAndView;
}
2.响应的view应该也是该请求的view。等同于void返回
@RequestMapping(method=RequestMethod.GET)
public Map<String, String> index(){
Map<String, String> map = new HashMap<String, String>();
map.put("1", "1");
//map.put相当于request.setAttribute方法
return map;
}
3.
@RequestMapping(method = RequestMethod.GET)
public String index(Model model) {
String retVal = "user/index";
List<User> users = userService.getUsers();
model.addAttribute("users", users);
return retVal;
}
4.当返回类型为Void的时候,则响应的视图页面为对应着的访问地址
@RequestMapping(method=RequestMethod.GET)
public void index(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("xxx", "xxx");
}