• 五、spingmvc之转发和重定向


     一般,控制器方法返回字符串类型的值会被当做视图名称处理(例如springmvc之helloworld 的视图),但是如果返回的字符串中带forward:或者redirect:前缀时,springMVC会对他们进行特殊处理:将forward:和redirect:当成指示符,其后的字符串作为URL处理;

    一、转发和重定向的区别:

    区别转发forward()重定向sendRedirect()
    根目录 包含项目访问地址 不包含项目访问地址
    地址栏 不会发生变化(只有第一次的请求地址) 会发生变化(重定向地址)
    哪里跳转 服务器端进行的跳转 浏览器端进行的跳转
    请求域中数据 不会丢失 会丢失

    二、根目录的区别:

    转    发:仅限于当前web应用内,"/"代表当前web应用的根目录

    重定向:可以定向到任何资源,"/"代表当前web站点的根目录

    实例验证:

    目录结构和配置方式:

    根目录的区别的项目目录结构

    RequestMappingTest:

     1 package handler;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 @RequestMapping("/springmvc")
     7 @Controller
     8 public class RequestMappingTest {
     9     @RequestMapping("/testForword")
    10     public String testForword() {
    11         System.out.println("testForword");
    12         return "forward:https://www.baidu.com/";
    13     }
    14 
    15     @RequestMapping("/testRedirect")
    16     public String testRedirect() {
    17         System.out.println("testRedirect");
    18         return "redirect:https://www.baidu.com/";
    19     }
    20 
    21 }

    index.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8" isErrorPage="false"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <a href="/springmvc/testForword">testForword</a>
    11     <br>
    12     <a href="/springmvc/testRedirect">testRedirect</a>
    13     <br>
    14 </body>
    15 </html>
    View Code

    success.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8" isErrorPage="true"%>
     3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
     4 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
     5 <%@    taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     6 <!DOCTYPE html>
     7 <html>
     8 <head>
     9 <meta charset="UTF-8">
    10 <title>Insert title here</title>
    11 </head>
    12 <body>
    13     <h4>success成功</h4>
    14     <br>
    15 </body>
    16 </html>
    View Code

    运行结果:

    index.jsp 的运行结果如图,点击testForword 显示 404且控制台打印testForword;点击testRedirect,显示百度首页且控制台打印testRedirect;表名重定向可以到任何资源,但是转发只能转发当前web资源;

    index.jsp效果图

    转发效果图

    重定向效果图

    三、地址栏&请求域数据&跳转路径区别:

    实例验证

    • 配置方式同上

    目录结构:

    RequestMappingTest:

    package handler;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.SessionAttributes;
    
    import entity.User;
    
    @SessionAttributes(value = { "user" })
    @RequestMapping("/springmvc")
    @Controller
    public class RequestMappingTest {
        @RequestMapping("/testForword")
        public String testForword() {
            System.out.println("testForword");
            return "forward:/helloworld/success";
        }
    
        @RequestMapping("/testRedirect")
        public String testRedirect() {
            System.out.println("testRedirect");
            return "redirect:/helloworld/success";
        }
    
        @ModelAttribute
        public void getUser(Map<String, Object> map) {
            User user = new User();
            user.setUserName("叶菲菲");
            user.setAge(30);
            map.put("user", user);
        }
    
    }

    Hello.java:

     1 package handler;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 @RequestMapping("/helloworld")
     7 @Controller
     8 public class Hello {
     9     private static final String SUCCESS = "success";
    10 
    11     @RequestMapping("/success")
    12     public String success() {
    13         System.out.println("success");
    14         return SUCCESS;
    15     }
    16 }
    View Code

    index.jsp:同上;

    success.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8" isErrorPage="true"%>
     3     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     4 <!DOCTYPE html>
     5 <html>
     6 <head>
     7 <meta charset="UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11     <h4>success成功</h4>
    12     <br>
    13     <c:if test="${not empty requestScope.user}">
    14         请求域:${requestScope.user}
    15     </c:if>
    16     <c:if test="${not empty sessionScope.user}">
    17         session:${sessionScope.user}
    18     </c:if>
    19 </body>
    20 </html>

    运行结果:

     点击testForword 跳转到success页面,显示sessionScope和requestScope中的user数据;且控制台打印testForword和success;此时地址栏显示的请求的地址;请求次数只有一次;只有testForword;

    点击testRedirect,跳转到success页面,显示sessionScope中的user数据;且控制台打印testRedirect和success;此时地址栏显示的请求的地址经过转发的地址;请求次数为两次;一次是testRedirect,一次是success;

    表明:

    • 请求域中数据在转发中不会发生变化,但是 在重定向时会发生改变;
    • 地址栏的地址 转发 不会更改 请求地址,重定向 会更改原来的请求地址;
    • 请求次数 转发为一次,重定向为2次;

    点击testForword效果

    点击testRedirect效果

  • 相关阅读:
    正则表达式
    jdbc,链接调用数据库的方法——例题
    python字符串操作
    python条件判断
    python运算符
    python中输入输出
    python关键字与标识符
    python变量
    python注释
    安装django报错一直安装不上?
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/16003950.html
Copyright © 2020-2023  润新知