一、从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
微信扫一扫
支付宝扫一扫