最近學習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");
}