SpringMVC之转发&重定向  1. 使用Servlet原生API进行数据输出到页面、转发和重定向 2. 通过SpringMVC实现转发和重定向(不需要视图解析器) 3. 使用SpringMVC进行转发和重定向(需要视图解析器)

  1.1 转发和重定向的区别

  a. 转发是一个请求一次响应,重定向是两次请求两次响应;

  b. 转发地址栏不发生变化,重定向地址栏发生变化(会显示重定向后的地址);

  c. 转发只能转发到本项目中其他控制器(在学习JavaWeb的时候,说的是:转发只能转发到本项目中其他Servlet),重定向不仅能重定向到本项目中的其他控制器(或其他Servlet),还能重定向到其他项目;

  d. 转发是服务器端的行为,只需给出转发的相对路径,重定向需要给出请求URI(即包含项目名)。

  1.2 说明

  a. 若需要地址栏发生变化,则使用重定向;

  b. 转发会携带数据,重定向不会携带数据。

  c. 转发可以转发到JavaWeb项目的/WEB-INF目录下面的页面,但是重定向不可以重定向到/WEB-INF目录下面的页面(资源),因为/WEB-INF目录下面的资源是不可以直接访问的,由于转发是服务器端行为,所以可以转发到

      /WEB-INF目录下面的资源,但是重定向相当于发送了两次请求,重定向到/WEB-INF目录下面的资源,就相当于直接访问了/WEN-INF目录下面的资源,所以是不被允许的。

  1.3 代码

SpringMVC之转发&重定向
 1. 使用Servlet原生API进行数据输出到页面、转发和重定向
2. 通过SpringMVC实现转发和重定向(不需要视图解析器)
3. 使用SpringMVC进行转发和重定向(需要视图解析器)

package com.sun.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 使用ServletAPI进行数据输出到界面、转发和重定向
 *
 * @author sunhongguang
 * @create 2020-11-25-23:04
 */
@Controller
@RequestMapping(path = "/c2")
public class ControllerTest2 {

    /**
     * 使用response向页面输出数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping(path = "/t1")
    public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().println(request.getSession().getId());
    }

    /**
     * 使用request.getRequestDispatcher(path).forward(request,response) 进行转发
     * 注意:转发的路径前面不用写项目名 (本项目的项目名称是/springmvc)
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping(path = "/t2")
    public void test2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("msg", request.getSession().getId());
        request.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(request, response);
    }

    /**
     * 使用request.getRequestDispatcher(path).forward(request,response) 进行转发,转发到本项目中的其他控制器(比如转发到 t2)
     * 注意:转发到其他控制器时,只能写相对路径(即:要转发的路径前面不能带 / )
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping(path = "/t2_1")
    public void test2_1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("t2").forward(request, response);
    }

    /**
     * 使用response.sendRedirect(paht)进行重定向
     *
     * @param request
     * @param response
     */
    @GetMapping(path = "/t3")
    public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("/springmvc/index.jsp");
    }

    /**
     * 使用response.sendRedirect(paht)进行重定向
     * 注意:要重定向的路径前面不能带 / ,即要使用相对路径
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping(path = "/t3_1")
    public void test3_1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("t3");
    }
}

2. 通过SpringMVC实现转发和重定向(不需要视图解析器)

package com.sun.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 通过SpringMVC实现转发和重定向(不需要视图解析器)
 * @author sunhongguang
 * @create 2020-11-26-0:04
 */
@Controller
@RequestMapping(path = "/c3")
public class ControllerTest3 {

    /**
     * 直接写要转发的路径(不用带项目名)
     * @return
     */
    @GetMapping(path = "/t1")
    public String test1(HttpServletRequest request){
        request.setAttribute("msg", request.getSession().getId());
        return "/WEB-INF/jsp/test.jsp";
    }

    /**
     * 直接写要转发的路径(不用带项目名,转发的路径前面写上forward)
     * @return
     */
    @GetMapping(path = "/t1_1")
    public String test1_1(HttpServletRequest request){
        request.setAttribute("msg", request.getSession().getId());
        return "forward:/WEB-INF/jsp/test.jsp";
    }

    /**
     * 转发到另外一个请求(比如转发到 /t1),不带forward
     * @return
     */
    @GetMapping(path = "/t1_2")
    public String test1_2(){
        return "t1";
    }

    /**
     * 转发到另外一个请求(比如转发到 /t1),带forward
     * @return
     */
    @GetMapping(path = "/t1_3")
    public String test1_3(){
        return "forward:t1";
    }

    /**
     * 重定向需要加上redirect,不用写项目名
     * @return
     */
    @RequestMapping("/t2")
    public String test2(){
        return "redirect:/index.jsp";
    }

    /**
     * 重定向需要加上redirect,
     * @return
     */
    @RequestMapping("/t2_1")
    public String test2_1(){
        return "redirect:t2";
    }
}

3. 使用SpringMVC进行转发和重定向(需要视图解析器)

package com.sun.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 使用SpringMVC进行转发和重定向(需要视图解析器)
 *
 * @author sunhongguang
 * @create 2020-11-26-22:03
 */
@Controller
@RequestMapping(path = "/c4")
public class ControllerTest4 {

    /**
     * 有视图解析器,并且使用@Controller注解和@xxxMapping注解,方法返回字符串时,默认是转发到返回的字符串对应的页面
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping(path = "/t1")
    public String test1(HttpServletRequest request, Model model) {
        model.addAttribute("msg", request.getSession().getId());
        return "test";
    }

    /**
     * 使用redirect关键字重定向到指定页面
     *
     * @return
     */
    @GetMapping(path = "/t2")
    public String test2() {
        return "redirect:/index.jsp";
    }

    /**
     * 使用redirect关键字重定向到另一个请求
     *
     * @return
     */
    @GetMapping(path = "/t3")
    public String test3() {
        return "redirect:t1";
    }
}