使用
RedirectView
重定向跳转到指定的后端控制器
import org.springframework.web.servlet.view.RedirectView;
@GetMapping("/redirect")
public RedirectView redirectToAnotherController() {
return new RedirectView("/targetControllerMapping");
}
使用
RedirectAttributes
传递参数并跳转到指定的后端控制器
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@GetMapping("/redirect")
public String redirectToAnotherController(RedirectAttributes redirectAttributes) {
redirectAttributes.addAttribute("paramName", "paramValue");
return "redirect:/targetControllerMapping";
}
请求转发跳指定controller
@GetMapping("/hello/forward")
public void helloForward(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/forward?method=forward");
requestDispatcher.forward(request,response);
}