springboot跳转页面和重定向,请求转发

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot跳转页面和重定向,请求转发

健康平安的活着   2022-05-23 我要评论

springboot跳转到指定页面

controller的写法

必须是templates下面的页面,不经过配置,无法直接跳转到public,static,等目录下的页面

package com.ljf.spring.boot.demo.controller; 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/04/01 10:26:05 
 * @Version: V1.0
 **/
@Controller
public class UserController {
    @RequestMapping("/api/show")
    public String showName(String userName,Model model){
        System.out.println("进入controller层了!!!"+userName);
         model.addAttribute("name",userName);
        return "index";//跳转到指定页面
    } 
}

springboot重定向和请求转发

springboot重定向

方式一:使用 "redirect" 关键字(不是指java关键字),注意:类的注解不能使用@RestController,要用@Controller;因为@RestController内含@ResponseBody,解析返回的是json串。不是跳转页面

@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public String test(@PathVariable String name) {
  return "redirect:/ceng/hello.html";
}

方式二:使用servlet 提供的API,注意:类的注解可以使用@RestController,也可以使用@Controller

@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public void test(@PathVariable String name, HttpServletResponse response) throws IOException {
  response.sendRedirect("/ceng/hello.html");
}

springboot的请求转发

方式一:使用 "forword" 关键字(不是指java关键字),注意:类的注解不能使用@RestController 要用@Controller

@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public String test(@PathVariable String name) {
  return "forword:/ceng/hello.html";
  }

方式二:使用servlet 提供的API,注意:类的注解可以使用@RestController,也可以使用@Controller

@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public void test(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
  request.getRequestDispatcher("/ceng/hello.html").forward(request,response);
}

springboot转发和重定向

springmvc重定向写法

@RequestMapping(“/test1”)
public String test1(){
return “index”;
}

转发是不需要手动加html的,此时springboot发现是转发,默认配置下他会自动去templates文件夹下找到对应的文件进行转发,如果return后写的是index.html会报404。

重定向

@RequestMapping(“/test2”)
public String test2(){
return “redirect:index1.html”;
}

首先,添加redirect:这个毋庸置疑是mvc的语法问题,其次这里需要注意的是需要手动添加.html,否则会报404

转发的特点

地址栏不发生变化,显示的是上一个页面的地址

请求次数:只有1次请求

根目录:http://localhost:8080/项目地址/,包含了项目的访问地址

请求域中数据不会丢失

在这里插入图片描述

重定向的特点

地址栏:显示新的地址

请求次数:2次

根目录:http://localhost:8080/ 没有项目的名字

请求域中的数据会丢失,因为是2次请求

疑问

问:什么时候使用转发,什么时候使用重定向?

如果要保留请求域中的数据,使用转发,否则使用重定向。

以后访问数据库,增删改使用重定向,查询使用转发。

问:转发或重定向后续的代码是否还会运行?

无论转发或重定向后续的代码都会执行

在这里插入图片描述

1、转发使用的是getRequestDispatcher()方法;重定向使用的是sendRedirect();

2、转发:浏览器URL的地址栏不变。重定向:浏览器URL的地址栏改变;

3、转发是服务器行为,重定向是客户端行为;

4、转发是浏览器只做了一次访问请求。重定向是浏览器做了至少两次的访问请求;

5、转发2次跳转之间传输的信息不会丢失,重定向2次跳转之间传输的信息会丢失(request范围)。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们