使用springboot跳转到指定页面和(重定向,请求转发的实例)

作者:健康平安的活着 时间:2021-10-21 11:09:24 

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/项目地址/,包含了项目的访问地址

请求域中数据不会丢失

使用springboot跳转到指定页面和(重定向,请求转发的实例)

重定向的特点

地址栏:显示新的地址

请求次数:2次

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

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

疑问

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

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

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

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

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

使用springboot跳转到指定页面和(重定向,请求转发的实例)

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

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

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

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

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

来源:https://blog.csdn.net/u011066470/article/details/115379182

标签:springboot,跳转,页面,重定向,请求转发
0
投稿

猜你喜欢

  • IDEA启动tomcat控制台中文乱码问题的解决方法(100%有效)

    2021-06-25 10:45:23
  • Android中获取资源 id 及资源 id 的动态获取

    2023-06-30 04:38:06
  • Java创建子线程的两种方法

    2023-11-24 07:00:05
  • 用Flutter开发自定义Plugin的方法示例

    2023-07-05 00:19:40
  • Android对话框AlertDialog详解

    2023-06-20 01:47:19
  • Java MongoDB数据库连接方法梳理

    2023-11-25 01:01:20
  • 使用Java桥接模式打破继承束缚优雅实现多维度变化

    2023-08-23 09:00:34
  • java实现马踏棋盘的算法

    2023-11-29 17:00:29
  • SpringBoot整合Activiti工作流框架的使用

    2022-03-02 12:32:44
  • Java Socket使用加密协议进行传输对象的方法

    2023-11-28 12:47:44
  • SpringBoot 如何实时刷新静态文件

    2023-11-02 13:27:09
  • ContentProvider启动流程示例解析

    2023-07-31 03:57:34
  • SpringBoot配置Email发送功能实例

    2022-03-07 20:52:20
  • Android编程之手机壁纸WallPaper设置方法示例

    2023-09-27 20:51:10
  • SpringBoot+Netty+WebSocket实现消息发送的示例代码

    2023-08-16 00:02:52
  • 关于Jsoup将相对路径转为绝对路径的方法

    2022-03-11 04:13:50
  • JAVA操作MongoDB数据库实例教程

    2023-11-18 13:22:27
  • java集合迭代器Iterator中的remove陷阱

    2022-06-17 16:11:54
  • Java特性队列和栈的堵塞原理解析

    2023-10-13 14:15:55
  • Java生成压缩文件的实例代码

    2023-02-04 21:11:09
  • asp之家 软件编程 m.aspxhome.com