- 控制器方法返回值类型
- 返回String字符串
- 返回页面名称字符串(视图名字)
return "success"; 经过视图解析器跳转到视图地址:/pages/success.jsp
- 返回转发字符串
//语法:"forward:完整转发路径" //注意:转发只能跳转到当前项目内的资源地址 //return "forward:/index.jsp"; //不走视图解析器,直接转发跳转具体的资源地址
- 返回重定向字符串
//语法:"redirect:完整重定向路径" //注意:路径中不能含有项目路径,因为springmvc自动给路径加上了项目路径 // 不需要使用request.getContextPath() // 可以跳转到外部资源,路径需要带有http协议的路径,比如:https://www.baidu.com // 路径中的第一个“/”,代表跳转到当前项目内的资源地址
- 返回void
//返回void @RequestMapping("/void") public void returnString(HttpServletRequest request, HttpServletResponse response){ try { //使用原生servlet的api的应用场景 //1.转发 //request.getRequestDispatcher("/index.jsp").forward(request,response); //2.重定向 //response.sendRedirect(request.getContextPath()+"/index.jsp"); //3.附件下载 //设置响应头:通知浏览器进行附件下载 response.setHeader("content-disposition","attachment;filename=6.jpg"); //获取当前项目“/down/6.jpg”的输入流 InputStream inputStream = request.getServletContext().getResourceAsStream("/down/6.jpg"); //使用commons-io工具类将收入流通过response的输出流输出给浏览器 IOUtils.copy(inputStream,response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }
- 返回ModelAndView对象
- 返回任意对象。(配置@ResponseBody注解实现转换为json字符串返回)
前提必须引入jackson支持包, 提交的数据类型必须设置为 application/json;charset=utf-8
坚持 - 返回String字符串
springMvc控制器方法返回值
最新推荐文章于 2022-11-04 13:27:59 发布