Springboot重定向传递参数

一、从Springboot启动jar传递参数

在开发Springboot应用时,有些情况下需要从启动jar命令中传递参数给应用。例如,我们要启动一个监听特定端口并响应指定请求路径的Web应用,那么此时就需要将端口和请求路径参数传递给Springboot应用,并在代码中获取并使用这些参数。传递参数的方式有多种,如使用Java系统属性、操作系统环境变量等。

Java系统属性传递参数示例:在启动命令中加上参数

java -Dserver.port=9999 -Drequest.path=/hello -jar my-application.jar

在代码中获取传递的参数:

@Value("${server.port}")
private int port;

@Value("${request.path}")
private String path;

二、Springboot页面跳转传递参数

在应用中,经常需要进行页面之间的跳转,并且有时需要把一些参数传递给目标页面。Springboot中页面跳转通常使用Controller进行控制,通过ModelAndView或者RedirectView两种方式进行页面跳转和参数传递。

使用ModelAndView传递参数示例:

@GetMapping("/user")
public ModelAndView getUser(){
    User user = new User("张三",20);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("user", user);
    modelAndView.setViewName("user");
    return modelAndView;
}

在user.html页面中获取传递的参数:

<div th:text="${user.name}"></div>

使用RedirectView传递参数示例:

@GetMapping("/login")
public RedirectView login(String username, String password){
    if(username.equals("admin") && password.equals("123456")){
        return new RedirectView("/user");
    }
    return new RedirectView("/login");
}

在/user路径下的Controller中获取传递的参数:

@GetMapping("/user")
public ModelAndView showUser(String username, String password){
    ModelAndView mnv = new ModelAndView();
    mnv.addObject("username", username);
    mnv.addObject("password", password);
    mnv.setViewName("user");
    return mnv;
}

三、Springboot重定向携带参数

Springboot中重定向是指将当前请求重定向到新的URL地址上,而重定向时需要传递一些参数给下一个页面。使用RedirectAttributes或者Session两种方式进行参数传递。

使用RedirectAttributes携带参数示例:

@PostMapping("/login")
public String login(String username, String password, RedirectAttributes ra){
    if(username.equals("admin") && password.equals("123456")){
        ra.addFlashAttribute("username",username);
        ra.addFlashAttribute("password",password);
        return "redirect:/user";
    }
    return "redirect:/login";
}

在/user路径下的Controller中获取传递的参数:

@GetMapping("/user")
public ModelAndView showUser(@ModelAttribute("username") String username, @ModelAttribute("password") String password){
    ModelAndView mnv = new ModelAndView();
    mnv.addObject("username", username);
    mnv.addObject("password", password);
    mnv.setViewName("user");
    return mnv;
}

使用Session携带参数示例:

@PostMapping("/login")
public String login(String username, String password, HttpSession session){
    if(username.equals("admin") && password.equals("123456")){
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        return "redirect:/user";
    }
    return "redirect:/login";
}

在/user路径下的Controller中获取传递的参数:

@GetMapping("/user")
public ModelAndView showUser(HttpSession session){
    ModelAndView mnv = new ModelAndView();
    mnv.addObject("username", session.getAttribute("username"));
    mnv.addObject("password", session.getAttribute("password"));
    mnv.setViewName("user");
    return mnv;
}

四、Springboot重定向带参数

使用重定向跳转页面时,可以在URL后面加上参数进行传递。如下面的示例:

@GetMapping("/test")
public String test(String name, HttpServletResponse response) throws IOException {
    response.sendRedirect("/user?name=" + URLEncoder.encode(name,"UTF-8"));
    return null;
}

在/user路径下的Controller中获取传递的参数:

@GetMapping("/user")
public ModelAndView showUser(@RequestParam String name){
    ModelAndView mnv = new ModelAndView();
    mnv.addObject("name", name);
    mnv.setViewName("user");
    return mnv;
}

五、Springboot参数传递与接收

Springboot中的参数传递方式有多种,如路径参数、请求参数、消息体参数等。路径参数和请求参数的传递与接收都非常简单。

路径参数示例:

@GetMapping("/users/{id}")
public User getUser(@PathVariable("id") int id){
    User user = userService.getUserById(id);
    return user;
}

请求参数示例:

@GetMapping("/users")
public List<User> getUsers(String name){
    List<User> users = userService.getUsersByName(name);
    return users;
}

六、Springboot重定向Vue传参数

在Springboot中重定向到Vue应用时,可以使用URL参数进行传递。在Vue应用端可以使用$route.params获取传递的参数。

Springboot重定向带参数示例:

@GetMapping("/redirect-to-vue")
public String redirectToVue(String name, HttpServletResponse response) throws IOException {
    response.sendRedirect("/vue?name=" + name);
    return null;
}

在Vue应用中获取传递的参数:

mounted() {
    console.log(this.$route.params.name);
}

七、Springboot重定向

在Springboot中重定向可以使用RedirectView或者ResponseEntity两种方式实现。

RedirectView示例:

@GetMapping("/redirect-to-page")
public RedirectView redirectToPage(){
    RedirectView redirectView = new RedirectView();
    redirectView.setUrl("/user");
    return redirectView;
}

ResponseEntity示例:

@GetMapping("/redirect-to-page")
public ResponseEntity redirectToPage(){
    HttpHeaders headers = new HttpHeaders();
    headers.set("Location", "/user");
    return new ResponseEntity(headers, HttpStatus.FOUND);
}

八、重定向能否传递参数

在Springboot中,重定向可以传递参数到目标页面,而且通过不同的方式进行参数传递。

九、重定向如何传递参数

在Springboot中,重定向可以使用URl参数、RedirectAttributes、Session等方式进行参数传递。

原创文章,作者:EDLGF,如若转载,请注明出处:https://www.506064.com/n/332004.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
EDLGFEDLGF
上一篇 2025-01-20 14:10
下一篇 2025-01-20 14:10

相关推荐

  • 三星内存条参数用法介绍

    本文将详细解释三星内存条上面的各种参数,让你更好地了解内存条并选择适合自己的一款。 一、容量大小 容量大小是内存条最基本的参数,一般以GB为单位表示,常见的有2GB、4GB、8GB…

    编程 2025-04-29
  • Python3定义函数参数类型

    Python是一门动态类型语言,不需要在定义变量时显示的指定变量类型,但是Python3中提供了函数参数类型的声明功能,在函数定义时明确定义参数类型。在函数的形参后面加上冒号(:)…

    编程 2025-04-29
  • Python input参数变量用法介绍

    本文将从多个方面对Python input括号里参数变量进行阐述与详解,并提供相应的代码示例。 一、基本介绍 Python input()函数用于获取用户输入。当程序运行到inpu…

    编程 2025-04-29
  • Spring Boot中发GET请求参数的处理

    本文将详细介绍如何在Spring Boot中处理GET请求参数,并给出完整的代码示例。 一、Spring Boot的GET请求参数基础 在Spring Boot中,处理GET请求参…

    编程 2025-04-29
  • Python函数名称相同参数不同:多态

    Python是一门面向对象的编程语言,它强烈支持多态性 一、什么是多态多态是面向对象三大特性中的一种,它指的是:相同的函数名称可以有不同的实现方式。也就是说,不同的对象调用同名方法…

    编程 2025-04-29
  • Python Class括号中的参数用法介绍

    本文将对Python中类的括号中的参数进行详细解析,以帮助初学者熟悉和掌握类的创建以及参数设置。 一、Class的基本定义 在Python中,通过使用关键字class来定义类。类包…

    编程 2025-04-29
  • Hibernate日志打印sql参数

    本文将从多个方面介绍如何在Hibernate中打印SQL参数。Hibernate作为一种ORM框架,可以通过打印SQL参数方便开发者调试和优化Hibernate应用。 一、通过配置…

    编程 2025-04-29
  • 从ga角度解读springboot

    springboot作为目前广受欢迎的Java开发框架,其中的ga机制在整个开发过程中起着至关重要的作用。 一、ga是什么 ga即Group Artifacts的缩写,它是Mave…

    编程 2025-04-29
  • 全能编程开发工程师必知——DTD、XML、XSD以及DTD参数实体

    本文将从大体介绍DTD、XML以及XSD三大知识点,同时深入探究DTD参数实体的作用及实际应用场景。 一、DTD介绍 DTD是文档类型定义(Document Type Defini…

    编程 2025-04-29
  • Python可变参数

    本文旨在对Python中可变参数进行详细的探究和讲解,包括可变参数的概念、实现方式、使用场景等多个方面,希望能够对Python开发者有所帮助。 一、可变参数的概念 可变参数是指函数…

    编程 2025-04-29

发表回复

登录后才能评论