广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用springboot跳转到指定页面和(重定向,请求转发的实例)
  • 800
分享到

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

2024-04-02 19:04:59 800人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot跳转到指定页面controller的写法springboot重定向和请求转发springboot重定向springboot的请求转发springboot转发和

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;
 

@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范围)。

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

--结束END--

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

本文链接: https://www.lsjlt.com/news/160156.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作