|
@@ -0,0 +1,91 @@
|
|
|
+package com.lovecoding.mvc;
|
|
|
+
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Spring MVC 重定向
|
|
|
+ * 操作成功! 中间页面
|
|
|
+ * 我们之前在 JSP 页面输出 301 302 状态码 进行重定向操作
|
|
|
+ */
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class RediRectController {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 我们访问 A页面 , 转向 B页面
|
|
|
+ * 举例说明
|
|
|
+ * 我们个人所得税退税
|
|
|
+ * 国家网站
|
|
|
+ * 我们填写表单=> save() 不展示页面的
|
|
|
+ * 重定向操作 save => msg 页面 通用消息展示页面
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ // 我们重新复习一下 JSP 的重定向
|
|
|
+ @GetMapping("/responseDemo")
|
|
|
+ public void responseDemo(HttpServletResponse resp){
|
|
|
+ try {
|
|
|
+ resp.sendRedirect("http://qq.com");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用ModelAndView 跳转
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/redirectModelAndView")
|
|
|
+ public ModelAndView redirectModelAndView( ModelAndView modelAndView){
|
|
|
+ //重定向到 responseDemo 页面
|
|
|
+ //由于 responseDemo 又重定向到了 腾讯
|
|
|
+ //最终我们能看到 qq.com 的页面
|
|
|
+ modelAndView.setViewName("redirect:responseDemo");
|
|
|
+ return modelAndView;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * URL 重定向 并且 携带参数
|
|
|
+ * @param attributes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/returnRedirectDemo")
|
|
|
+ public String returnRedirectDemo(RedirectAttributes attributes){
|
|
|
+ attributes.addAttribute("name", "张三");
|
|
|
+ attributes.addAttribute("age", 108);
|
|
|
+ return "redirect:defpage";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认跳转页面
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/defpage")
|
|
|
+ public String defPage(){
|
|
|
+ return "/defPage.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求转发
|
|
|
+ * JSP 时代 也用过请求转发
|
|
|
+ * SPringMVC 也可以使用 forward 进行页面转发
|
|
|
+ */
|
|
|
+ @GetMapping("/forwardDemo")
|
|
|
+ public String forwardDemo(){
|
|
|
+
|
|
|
+ //JSP 时代的代码
|
|
|
+ //req.getRequestDispatcher("/index.jsp").forward(req);
|
|
|
+ //SpringMVC时代的代码
|
|
|
+ //return "/index.jsp";
|
|
|
+ //默认return 就是请求转发
|
|
|
+ return "forward:/forwardDemo.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|