SpringMVC

SpringMVC的跳转方式

ModelAndView

@RequestMapping("test3")
public ModelAndView test3(ModelAndView modelAndView) {
    //指定要跳转的页面
    modelAndView.setViewName("a");
    return modelAndView;
}

ServletAPI

//写数据
@RequestMapping("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","t3");
    req.getRequestDispatcher("a.jsp").forward(req,rsp);
}

SpringMVC

  • 下面的三个方式,不能有视图解析器
//请求转达
@RequestMapping("test5")
public String test5() {
    return "/a.jsp";
}
//请求转达
@RequestMapping("test6")
public String test6() {
    return "forward:/a.jsp";
}
//重定向
@RequestMapping("test7")
public String test7() {
    return "redirect:/a.jsp";
}
  • 这种的需要视图解析器
@RequestMapping("test1")
public String test1(){
    //转发
    return "test";
}
不经风雨,怎见彩虹?
原文地址:https://www.cnblogs.com/MLYR/p/14486373.html