一、從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/zh-hant/n/332004.html