Servlet API

  • 不用配置视图解析器
 @RequestMapping("/tiaozhuan")
    public void test(HttpServletRequest req, HttpServletResponse resp) throws Exception {
         resp.getWriter().println("这是跳转的新页面");	->输出流
         //resp.sendRedirect("index.jsp");或者.html	->重定向
         //req.getRequestDispatcher("index.jsp").forward(req,resp);	->转发
    }

返回值类型String

  • 不用配置视图解析器
@RequestMapping("/tiaozhuan")
    public String test(){
        return "index.jsp";        //转发
        return "forward:index.jsp";        //转发
        return "redirect:index.jsp";        //重定向
    }
  • 使用默认视图解析器,并添加前缀后缀
 @RequestMapping("/tiaozhuan")
    public String hello1(){
        return "index";        //转发
        return "forward:index";        //转发
        return "redirect:index";        //重定向  也可以跳转到controller
    }

在这里插入图片描述
在这里插入图片描述
配置完前缀和后缀之后,会导致请求页面都会自动的添加前缀和后缀,可能会导致如下404的问题,解决办法:把jsp统统要么都放在WEB-INF下,要么都放在外面。
在这里插入图片描述

返回值类型ModelAndView

  • 默认的视图解析器
 @RequestMapping("/tiaozhuan")
public ModelAndView test(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        ModelAndView mv = new ModelAndView();
        //域中添加数据
        mv.addObject("msg","跳转成功");
        //视图名称跳转
        mv.setViewName("index");
        return mv;
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐