java控制器返回视图类型_SpringMVC中的Controller方法的(返回值/参数类型)
一. Controller方法的返回值:
1、 返回的ModelAndView
ModelAndView 存放数据, addObject(),往model(request域)添加数据
ModelAndView 添加逻辑视图名, setViewName(), 经过视图解析器,得到物理视图, 转发到物理视图
@RequestMapping("/getUser.action")public ModelAndView getUser(@RequestParam(name=“userId”,required = true)Integer id) throwsException{
System.out.println(“id=”+id);
ModelAndView modelAndView= newModelAndView();
User user=userService.queryOne(id);
modelAndView.addObject(“user”, user);
modelAndView.setViewName(“userinfo”);returnmodelAndView;
}
2、 String类型, 返回的视图
a. 逻辑视图名, 经过视图解析器,得到物理视图, 转发
@RequestMapping("/index.action")publicString toIndex() {return “index”;
b. redirect:资源路径, 不经过视图解析器,要求这个资源路径写完整的路径: /开头, 表示/项目名 重定向到资源
@RequestMapping("/index.action")publicString toIndex() {//重定向到index.jsp, 完整的路径
return “redirect:/jsp/index.jsp”;
c. forward:资源路径, 不经过视图解析器,要求这个资源路径写完整的路径: /开头,表示/项目名 转发向到资源
@RequestMapping("/index.action")publicString toIndex() {return “forward:/jsp/index.jsp”;
d.响应给前端的字符串,(数据),需要结合@ResponseBody
//将user对象以json的格式响应给前端页面
@RequestMapping("/queryUserByCondition.action")
@ResponseBodypublic User queryUserByCondition(User user) throwsException{returnuser;
}
3、Java对象
需要结合@ResponseBody, 发生的数据,(json)
主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),只能是post提交,get没有请求体
@RequestMapping("/queryUserByCondition.action")
@ResponseBodypublic User queryUserByCondition( @RequestBody User user) throwsException{returnuser;
}
4、 void, 默认逻辑视图名
controller的@RequestMapping() 前缀+ 方法名, 很少使用