首页 课程 师资 教程 报名

2种方法能够实现Java页面跳转

  • 2022-09-06 08:04:48
  • 3188次 动力节点

页面跳转分类有两种:重定向和转发,即redirect和forward。

具体区别和含义最后介绍,先给出使用方法。

1.重定向redirect

第一种方式:controller中返回值为String

public String login(HttpServletRequest req, HttpServletResponse resp)
return “redirect:http://localhost:8080/index”;

第二种方式:controller中返回值为void

public void login(HttpServletRequest req, HttpServletResponse resp)
resp.sendRedirect(“http://localhost:8080/index”);

第三种方式:controller中返回值为ModelAndView

return new ModelAndView(“redirect:/toList”);

2.转发forward

request.getRequestDispatcher("/index").forward(request, response);

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交