SpringMVC-视图跳转

156 阅读1分钟

ServletAPI

通过设置ServletAPI , 不需要视图解析器 .

1、通过HttpServletResponse进行输出

2、通过HttpServletResponse实现重定向(改变url)

3、通过HttpServletResponse实现转发

@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    rsp.getWriter().println("Hello,Spring BY servlet API");
}

@RequestMapping("/result/t2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    rsp.sendRedirect("/index.jsp");
}

@RequestMapping("/result/t3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
    //转发
    req.setAttribute("msg","/result/t3");
    req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}

SpringMVC(无需视图解析器)

@RequestMapping("/rsm/t1")
public String test1(){
    //转发
    return "/index.jsp";
}

@RequestMapping("/rsm/t2")
public String test2(){
    //转发二
    return "forward:/index.jsp";
}

@RequestMapping("/rsm/t3")
public String test3(){
    //重定向
    return "redirect:/index.jsp";
}