SpringMVC之转发&重定向

 1. 使用Servlet原生API进行数据输出到页面、转发和重定向

  1.1 转发和重定向的区别

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

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

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

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

  1.2 说明

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

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

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

      /WEB-INF目录下面的资源,可是重定向至关于发送了两次请求,重定向到/WEB-INF目录下面的资源,就至关于直接访问了/WEN-INF目录下面的资源,因此是不被容许的。学习

  1.3 代码

 

 

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";
    }
}
相关文章
相关标签/搜索