springboot重定向传递参数
- 格式:docx
- 大小:37.09 KB
- 文档页数:4
springboot重定向⽅式(redirect前缀)⽬录springboot 重定向(redirect前缀)相关注解⽰例Spring 重定向(Redirect)指南为什么要重定向?使⽤ RedirectView 重定向结论springboot 重定向(redirect前缀)相关注解@ModelAttribute:读取modelAndView中的数据@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ModelAttribute {@AliasFor("name")String value() default "";@AliasFor("value")String name() default "";boolean binding() default true;}⽰例@RestControllerpublic class Hello3Controller {@RequestMapping("/hello2")public ModelAndView hello2(){ModelAndView mv=new ModelAndView();mv.setViewName("redirect:/redirect2");mv.addObject("attributeName","海贼王");Person person=new Person();person.setName("⽠⽥李下");person.setAge(20);mv.addObject("person",person);return mv;}@RequestMapping("/redirect2")public String redirect2(String attributeName,@ModelAttribute("attributeName") String name,Person person,ModelAndView mv){System.out.println(attributeName+" "+name);System.out.println(person);System.out.println(mv.getModelMap().getAttribute("attributeName"));return "redirect2";}}控制台输出海贼王海贼王Person(name=null, age=null)null说明:直接在⽅法体内获取数据,参数可以⾃动映射,也可使⽤@ModelAttribute获取数据;这种⽅式只能传递字符串,pojo 对象不能传递Spring 重定向(Redirect)指南为什么要重定向?让我们先来考虑在 Spring 应⽤程序中为什么您可能需要做⼀个重定向的原因。
SpringBoot集成thymeleaf中onclick传参的各种方法别再被坑了我被在Spring Boot中,集成Thymeleaf并使用onclick传参可以通过以下几种方法实现:1. 使用Thymeleaf内置变量:Thymeleaf提供了一些内置的变量,可以在模板中直接使用。
其中,$this代表当前元素,$event代表当前事件。
可以通过这两个变量来获取传递的参数。
HTML模板:```html<button th:onclick="'handleClick(' + $this.id + ')'"th:id="${item.id}">点击按钮</button>```Controller:```java//处理点击事件return "redirect:/page";```2. 使用 Thymeleaf 提供的th:attr属性:通过th:attr属性设置一些元素的属性值,可以动态传递参数。
HTML模板:```html<button th:attr="onclick='handleClick(\'' + ${item.id} +'\')'">点击按钮</button>```Controller:```java//处理点击事件return "redirect:/page";```3. 使用 Thymeleaf 提供的th:data属性:通过th:data属性设置一些元素的数据属性,可以动态传递参数。
HTML模板:```html<button th:data-itemId="${item.id}"onclick="handleClick(this.getAttribute('data-itemId'))">点击按钮</button>```Controller:```java//处理点击事件return "redirect:/page";```4. 使用 Thymeleaf 提供的th:each遍历集合:通过遍历集合的方式,获取每个元素的属性值来传递参数。
springboot传参和获取参数方式获取和传递参数是Spring Boot程序最常见的操作之一。
下面我们详细介绍Spring Boot的传参和获取参数方式:一、传递参数1. PathVariablePathVariable可以从请求URL中获取参数,并将参数值绑定到处理器方法参数上,以@PathVariable注解指定参数名称。
2. RequestParamRequestParam可以从请求参数中获取参数,将参数值绑定到处理器方法参数上,以@RequestParam注解指定即可。
3. RequestBodyRequestBody可以从请求参数中获取参数,并将获取的参数值绑定到处理器方法参数上,以@RequestBody注解指定即可。
4. ModelAttributeModelAttribute可以从请求参数中获取参数,并将获取的参数值绑定到处理器方法参数上,以@ModelAttribute注解指定即可。
二、获取参数1. @PathVariable方式从请求URL中获取参数,以@PathVariable注解指定参数名称,以实例:@PathVariable(“name”)进行指定,即使用name变量即可接受请求URL中的name参数。
2. @RequestParam方式从请求参数中获取参数,以@RequestParam注解指定参数名称,以实例:@RequestParam(“name”)进行指定,即使用name变量即可接受请求URL中的name参数。
3. @RequestBody方式从请求参数中获取参数,以@RequestBody注解指定,即可接受请求参数中的所有内容。
4. @CookieValue方式从Cookie中获取参数,以@CookieValue注解指定需要取值的Cookie 名称,即可接受Cookie中的指定值。
以上就是Spring Boot传参和获取参数的方式。
通过以上不同的获取参数方式,可以让我们更加便捷地处理参数,快速构建完整应用。
java重定向携带参数
在Java中,重定向通常指的是将请求从一个URL重定向到另一个URL。
如果你想在重定向时携带参数,你可以使用查询参数(query parameters)的方式。
以下是一个简单的示例,展示了如何在Java Servlet中重定向到另一个URL并携带参数:
```java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取参数
String param1 = ("param1");
String param2 = ("param2");
// 构建重定向的URL,并添加查询参数
String redirectUrl = " + param1 + "¶m2=" + param2;
// 重定向到目标URL
(redirectUrl);
}
```
在这个例子中,我们从请求中获取了两个参数(`param1`和`param2`),然后将它们添加到重定向的URL中作为查询参数。
最后,我们使用`()`方法将请求重定向到目标URL。
注意:重定向时,客户端(例如浏览器)会发送一个新的GET请求到目标URL。
因此,所有的参数和表单数据都将在新的请求中发送。
基于springbootredirect重定向路径问题总结⽬录SpringMVC重定向视图RedirectView⼩分析前⾔实例讲解Controller代码我们通过firebug看下路径:总结SpringMVC重定向视图RedirectView⼩分析前⾔SpringMVC是⽬前主流的Web MVC框架之⼀。
本⽂所讲的部分内容跟SpringMVC的视图机制有关,SpringMVC的视图机制请参考楼主的另⼀篇博客:RedirectView这个视图是跟重定向相关的,也是重定向问题的核⼼,我们来看看这个类的源码。
路径构造完毕之后使⽤reponse进⾏sendRedirect操作。
实例讲解Controller代码@Controller@RequestMapping(value = “/redirect”)public class TestRedirectController {@RequestMapping("/test1")public ModelAndView test1() {view.setViewName("redirect:index");return view;}@RequestMapping("/test2")public ModelAndView test2() {view.setViewName("redirect:login");return view;}@RequestMapping("/test3")public ModelAndView test3(ModelAndView view) {view.setViewName("redirect:/index");return view;}@RequestMapping("/test4")public ModelAndView test4(ModelAndView view) {view.setView(new RedirectView("/index", false));return view;}@RequestMapping("/test5")public ModelAndView test5(ModelAndView view) {view.setView(new RedirectView("index", false));return view;}@RequestMapping("/test6/{id}")public ModelAndView test6(ModelAndView view, @PathVariable("id") int id) {view.setViewName("redirect:/index{id}");view.addObject(“test”, “test”);return view;}@RequestMapping("/test7/{id}")public ModelAndView test7(ModelAndView view, @PathVariable("id") int id) {RedirectView redirectView = new RedirectView("/index{id}");redirectView.setExpandUriTemplateVariables(false);redirectView.setExposeModelAttributes(false);view.setView(redirectView);view.addObject("test", "test");return view;}先看test1⽅法,返回值 “redirect:index” ,那么会使⽤重定向。
springboot服务端获取前端传过来的参数7种⽅式下⾯为7种服务端获取前端传过来的参数的⽅法1、直接把表单的参数写在Controller相应的⽅法的形参中,适⽤于GET 和 POST请求⽅式这种⽅式不会校验请求⾥是否带参数,即下⾯的username和password不带也会响应成功@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping("/addUser1")public String addUser1(String username,String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "success";}}测试代码POST请求⽅式<script>var xhr = new XMLHttpRequest()xhr.open('POST', 'http://localhost:8080/tools/addUser1') // 设置请求⾏xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')xhr.send('username=zhangsan&password=123') // 以 urlencoded 格式设置请求体xhr.onload=function(){if(xhr.readyState!==4) returnconsole.log(xhr.responseText)}</script>GET请求⽅式:<script>var xhr = new XMLHttpRequest()xhr.open('GET', 'http://localhost:8080/tools/addUser1?username=zhangsan&password=123') // 设置请求⾏xhr.send()xhr.onload=function(){if(xhr.readyState!==4) returnconsole.log(xhr.responseText)}</script>2、通过HttpServletRequest接收,适⽤于GET 和 POST请求⽅式通过HttpServletRequest对象获取请求参数@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping("/addUser2")public String addUser2(HttpServletRequest request) {String username=request.getParameter("username");String password=request.getParameter("password");System.out.println("username is:"+username);System.out.println("password is:"+password);return "success";}}测试代码同上3、通过⼀个bean来接收,适⽤于GET 和 POST请求⽅式(1)建⽴⼀个和表单中参数对应的bean@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic class DemoUser {private String username;private String password;}(2)⽤这个bean来封装接收的参数@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping("/addUser3")public String addUser3(DemoUser user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "success";}}测试代码同上4、通过@PathVariable获取路径中的参数,适⽤于GET请求 通过注解获取url参数@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)public String addUser4(@PathVariable String username,@PathVariable String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "success";}}测试代码<script>var xhr = new XMLHttpRequest()xhr.open('GET', 'http://localhost:8080/tools/addUser4/username=zhangsan/password=123') // 设置请求⾏xhr.send()xhr.onload=function(){if(xhr.readyState!==4) returnconsole.log(xhr.responseText)}</script>⾃动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即⼊参后username=zhangsan、password=1235、使⽤@ModelAttribute注解获取参数,适⽤于POST请求@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping(value="/addUser5",method=RequestMethod.POST)public String addUser5(@ModelAttribute("user") DemoUser user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "success";}}测试代码<script>var xhr = new XMLHttpRequest()xhr.open('POST', 'http://localhost:8080/tools/addUser5') // 设置请求⾏xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')xhr.send('username=zhangsan&password=123')xhr.onload=function(){if(xhr.readyState!==4) returnconsole.log(xhr.responseText)}</script>6、⽤注解@RequestParam绑定请求参数到⽅法⼊参,适⽤于GET 和 POST请求⽅式添加@RequestParam注解,默认会校验⼊参,如果请求不带⼊参则会报错,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false) ,这样就不会校验⼊参,于第⼀种请求⽅式⼀样@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping(value="/addUser6",method=RequestMethod.GET)public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "success";}}测试代码同上7、⽤注解@RequestBody绑定请求参数到⽅法⼊参,⽤于POST请求@RequestBody主要⽤来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)@RestController@RequestMapping("/tools")public class InnerController {@RequestMapping(value="/addUser7",method=RequestMethod.POST)public String addUser7(@RequestBody DemoUser user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "success";}}测试代码:请求头需要指定为json类型<script>var xhr = new XMLHttpRequest()xhr.open('POST', 'http://localhost:8080/tools/addUser7') // 设置请求⾏xhr.setRequestHeader('Content-Type','application/json')xhr.send('{"username":"zhangsan","password":"123"}')xhr.onload=function(){if(xhr.readyState!==4) returnconsole.log(xhr.responseText)}</script>DemoUser这个类为⼀个实体类,⾥⾯定义的属性与URL传过来的属性名⼀⼀对应。
springboot实现转发和重定向1、转发⽅式⼀:使⽤ "forword" 关键字(不是指java关键字),注意:类的注解不能使⽤@RestController 要⽤@Controller1 2 3 4@RequestMapping(value="/test/test01/{name}", method = RequestMethod.GET)public String test(@PathVariable String name) {return"forword:/ceng/hello.html";}⽅式⼆:使⽤servlet 提供的API,注意:类的注解可以使⽤@RestController,也可以使⽤@Controller1 2 3 4@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);}2、重定向⽅式⼀:使⽤ "redirect" 关键字(不是指java关键字),注意:类的注解不能使⽤@RestController,要⽤@Controller1 2 3 4@RequestMapping(value="/test/test01/{name}", method = RequestMethod.GET)public String test(@PathVariable String name) {return"redirect:/ceng/hello.html";}⽅式⼆:使⽤servlet 提供的API,注意:类的注解可以使⽤@RestController,也可以使⽤@Controller1 2 3 4@RequestMapping(value="/test/test01/{name}", method = RequestMethod.GET)public void test(@PathVariable String name, HttpServletResponse response) throws IOException { response.sendRedirect("/ceng/hello.html");}使⽤API进⾏重定向时,⼀般会在url之前加上:request.getContextPath()。
SpringMVC向页面传递参数的4种方式1、使用HttpServletRequest和Session 然后setAttribute(),就和Servlet 中一样request.setAttribute(“user”,user_data);2、使用ModelAndView对象@RequestMapping("/login.do")publicModelAndView login(String name,String pass){User user = userService.login(name,pwd);Map<String,Object> data = new HashMap<String,Object>();data.put("user",user);return newModelAndView("success",data);}3、使用ModelMap对象ModelMap数据会利用HttpServletRequest的Attribute传值到success.jsp中@RequestMapping("/login.do")public String login(String name,String pass ,ModelMapmodelMap){User user =userService.login(name,pwd);modelMap.addAttribute("user",user);modelMap.put("name",name);return "success";}Session存储,可以利用HttpServletReequest的getSession()方法@RequestMapping("/login.do")Public String login (String name,Stringpwd,ModelMapmodel,HttpServletRequest request) {User user = serService.login(name,pwd);HttpSession session = request.getSession();session.setAttribute("user",user);model.addAttribute("user",user);return "success";}4、使用@ModelAttribute注解@ModelAttribute数据会利用HttpServletRequest的Attribute传值到success.jsp中@RequestMapping("/login.do")public String login(@ModelAttribute("user") User user){return "success";}@ModelAttribute("name")public String getName(){return name;}Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作A、使用RedirectViewpublicModelAndView login(){RedirectView view = new RedirectView("regirst.do");return newModelAndView(view);}B、使用redirect:前缀public String login(){return "redirect:regirst.do";}。
SpringMVC(三)控制器获取页⾯请求参数以及将控制器数据传递给页⾯和实现重定向的⽅式⾸先做好环境配置在mvc.xml⾥进⾏配置 1.开启组件扫描 2.开启基于mvc的标注 3.配置试图处理器1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="/schema/beans"3 xmlns:xsi="/2001/XMLSchema-instance"4 xmlns:context="/schema/context"5 xmlns:lang="/schema/lang"6 xmlns:mvc="/schema/mvc"7 xmlns:util="/schema/util"8 xmlns:task="/schema/task"9 xmlns:aop="/schema/aop"10 xsi:schemaLocation="/schema/mvc /schema/mvc/spring-mvc-4.1.xsd11 /schema/task /schema/task/spring-task-4.1.xsd12 /schema/beans /schema/beans/spring-beans.xsd13 /schema/context /schema/context/spring-context-4.1.xsd14 /schema/lang /schema/lang/spring-lang-4.1.xsd15 /schema/aop /schema/aop/spring-aop-4.1.xsd16 /schema/util /schema/util/spring-util-4.1.xsd">17 <!-- 开启组件扫描 -->18 <context:component-scan base-package="com.xcz"></context:component-scan>19 <!-- 开启mvc标注 -->20 <mvc:annotation-driven></mvc:annotation-driven>21 <!-- 配置视图处理器 -->22 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">23 <property name="prefix" value="/WEB-INF/"></property>24 <property name="suffix" value=".jsp"></property>25 </bean>26 </beans>mvc.xml在web.xml配置 1.配置请求参数如⼊⼝ 2.配置初始化参数1 <?xml version="1.0" encoding="UTF-8"?>2 <web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd" version="3.1">3 <display-name>SpringMVC-03</display-name>4 <welcome-file-list>5 <welcome-file>index.html</welcome-file>6 <welcome-file>index.htm</welcome-file>7 <welcome-file>index.jsp</welcome-file>8 <welcome-file>default.html</welcome-file>9 <welcome-file>default.htm</welcome-file>10 <welcome-file>default.jsp</welcome-file>11 </welcome-file-list>12 <!-- 配置请求⼊⼝ -->13 <servlet>14 <servlet-name>SpringMVC</servlet-name>15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>16 <!-- 配置初始化参数 -->17 <init-param>18 <param-name>contextConfigLocation</param-name>19 <param-value>classpath:mvc.xml</param-value>20 </init-param>21 <load-on-startup>1</load-on-startup>22 </servlet>23 <servlet-mapping>24 <servlet-name>SpringMVC</servlet-name>25 <url-pattern>*.do</url-pattern>26 </servlet-mapping>27 </web-app>web.xml控制器获取页⾯请求参数⽅式如下: 1.使⽤HttpServletRequest获取直接定义 HttpServletRequest参数 2.直接把请求参数的名字定义成控制器的参数名 3.当页⾯参数和控制器参数不⼀致可以使⽤ @RequestParam("页⾯参数名"),加在控制器⽅法对应的参数上1package com.xcz.controller;23import javax.servlet.http.HttpServletRequest;45import org.springframework.stereotype.Controller;6import org.springframework.ui.Model;7import org.springframework.ui.ModelMap;8import org.springframework.web.bind.annotation.RequestMapping;9import org.springframework.web.bind.annotation.RequestParam;10import org.springframework.web.servlet.ModelAndView;1112 @Controller13public class LoginController {14// 获取参数⽅式⼀15 @RequestMapping("/login.do")16public String login(HttpServletRequest request) {17 String username = request.getParameter("username");18 String password = request.getParameter("password");19 System.out.println(username + ":" + password);20return "login";21 }2223// 获取参数⽅式⼆24 @RequestMapping("/login2.do")25public String login2(String username, String password) {26 System.out.println(username + ":" + password);27return "login";2930// 获取参数⽅式三31 @RequestMapping("/login3.do")32public String login3(@RequestParam("username") String uname, @RequestParam("password") String pwd) {33 System.out.println(uname + ":" + pwd);34return "login";35 }36 }LoginController1 <%@ page language="java" contentType="text/html; charset=utf-8"2 pageEncoding="utf-8"%>3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">4 <html>5 <head>6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">7 <title>Insert title here</title>8 </head>9 <body>10 <form action="${pageContext.request.contextPath }/login8.do"> <!--${pageContext.request.contextPath }动态获取路径 --> 11账号:<input type="text" name="username" ><br>12密码:<input type="text" name="password" ><br>13 <input type="submit" value="登录"><br>14 </form>15 </body>16 </html>login.lsp控制器中数据传递给页⾯的⽅式如下: 1.使⽤ request session application 这些域对象传输 2.使⽤ModelAndView来传输数据 //mav.getModel().put("username", username); mav.getModelMap().addAttribute("username", username); 3.使⽤ Model 来传输数据 4.使⽤ModelMap 进⾏传参1package com.xcz.controller;23import javax.servlet.http.HttpServletRequest;45import org.springframework.stereotype.Controller;6import org.springframework.ui.Model;7import org.springframework.ui.ModelMap;8import org.springframework.web.bind.annotation.RequestMapping;9import org.springframework.web.bind.annotation.RequestParam;10import org.springframework.web.servlet.ModelAndView;1112 @Controller13public class LoginController {14// 將控制器中的数据传给页⾯⽅式⼀15 @RequestMapping("/login4.do")16public String login4(@RequestParam("username") String uname, @RequestParam("password") String pwd,17 HttpServletRequest request) {18 System.out.println(uname + ":" + pwd);19 request.setAttribute("username", uname);20return "main";21 }2223// 將控制器中的数据传给页⾯⽅式⼆24 @RequestMapping("/login5.do")25public ModelAndView login5(@RequestParam("username") String uname, @RequestParam("password") String pwd) {26 System.out.println(uname + ":" + pwd);27 ModelAndView mav = new ModelAndView();28 mav.setViewName("main");29 mav.getModel().put("username", uname);30return mav;31 }3233// 將控制器中的数据传给页⾯⽅式三34 @RequestMapping("/login6.do")35public ModelAndView login6(@RequestParam("username") String uname, @RequestParam("password") String pwd,36 ModelAndView mav) {37 System.out.println(uname + ":" + pwd);38 mav.setViewName("main");39 mav.getModelMap().addAttribute("username", uname);40// mav.getModelMap().put("username", uname);41return mav;42 }4344// 將控制器中的数据传给页⾯⽅式四45 @RequestMapping("/login7.do")46public String login7(@RequestParam("username") String uname, @RequestParam("password") String pwd, Model model) {47 System.out.println(uname + ":" + pwd);48 model.addAttribute("username", uname);49return "main";50 }5152//將控制器中的数据传给页⾯⽅式五53 @RequestMapping("/login8.do")54public String login8(@RequestParam("username") String uname, @RequestParam("password") String pwd,ModelMap map) {55 System.out.println(uname + ":" + pwd);56 map.put("username", uname);57return "main";58 }59 }LoginController1 <%@ page language="java" contentType="text/html; charset=utf-8"2 pageEncoding="utf-8"%>3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">4 <html>5 <head>6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">7 <title>Insert title here</title>8 </head>9 <body>10 <form action="${pageContext.request.contextPath }/login8.do"> <!--${pageContext.request.contextPath }动态获取路径 --> 11账号:<input type="text" name="username" ><br>12密码:<input type="text" name="password" ><br>13 <input type="submit" value="登录"><br>14 </form>15 </body>16 </html>1 <%@ page language="java" contentType="text/html; charset=utf-8"2 pageEncoding="utf-8"%>3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">4 <html>5 <head>6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">7 <title>Insert title here</title>8 </head>9 <body>10 <h1>欢迎${username }来访</h1>11 </body>12 </html>main.jsp实现重定向⽅式如下: 1.当控制器⽅法返回String时return"redirect:路径"; 默认是转发,转发的结果直接交给ViewResolver可以通过加forward:来继续处理,⽽不交给ViewResolver 2.当控制器⽅法返回 ModelAndView 时使⽤RedirectView 完成重定向(/代表项⽬名前⾯的部分不包含项⽬名)1package com.xcz.controller;23import javax.servlet.http.HttpServletRequest;4import org.springframework.stereotype.Controller;5import org.springframework.web.bind.annotation.RequestMapping;6import org.springframework.web.bind.annotation.RequestParam;7import org.springframework.web.servlet.ModelAndView;8import org.springframework.web.servlet.view.RedirectView;910 @Controller11public class LoginController {12private static final String URL = "toMain.do";13 @RequestMapping("/toLogin.do")14public String toLogin() {15return "login";16 }17 @RequestMapping("/toMain.do")18public String toMain() {19return "main";20 }21// 实现重定向⽅式⼀22 @RequestMapping("/login.do")23public String login(@RequestParam("username") String uname,@RequestParam("password") String pwd,HttpServletRequest request) {24 System.out.println(uname + ":" + pwd);25 request.setAttribute("usernameq", uname);26 request.setAttribute("password", pwd);27//return "redirect:/toMain.do"; //使⽤redirect: 直接重定向,导致数据丢失,所以页⾯⽆法获取28return "forward:/toMain.do"; //使⽤forward: 先转发后跳转到main.jsp,不会导致数据丢失,所以页⾯可以获取控制器数据29 }30// 实现重定向⽅式⼆31 @RequestMapping("/login1.do")32public ModelAndView login1(@RequestParam("username") String uname,@RequestParam("password") String pwd,HttpServletRequest request) {33 System.out.println(uname + ":" + pwd); //打印后台数据34 String path = request.getServletContext().getContextPath(); //获取项⽬名前⾯的路径35 ModelAndView mView = new ModelAndView();36 RedirectView rView = new RedirectView(path + "/" + URL); //将路径和项⽬名进⾏拼接起来37 mView.setView(rView);38 mView.getModelMap().addAttribute("username", uname); //将控制器的数据传给页⾯39return mView;40 }41 }LoginController1 <%@ page language="java" contentType="text/html; charset=utf-8"2 pageEncoding="utf-8"%>3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">4 <html>5 <head>6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">7 <title>Insert title here</title>8 </head>9 <body>10 <form action="${pageContext.request.contextPath }/login1.do"> <!--${pageContext.request.contextPath }动态获取路径 -->11账号:<input type="text" name="username" ><br>12密码:<input type="text" name="password" ><br>13 <input type="submit" value="登录"><br>14 </form>15 </body>16 </html>login.jsp1 <%@ page language="java" contentType="text/html; charset=utf-8"2 pageEncoding="utf-8"%>3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">4 <html>5 <head>6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">7 <title>Insert title here</title>8 </head>9 <body>10 <h1>欢迎${ername }来访</h1>11 </body>12 </html>main.jsp。
jsp中利⽤response.senddirect(str)重定向,传递参数新思路⽤Servlet进⾏请求重定向,参数传递好办,直接⽤request.setAttribute(str1,str2);但是如果不⽤Servlet ⽽是直接⽤jsp进⾏转发呢?我们⾸先要知道请求的重定向:在最终的Servlet中,request对象和中转的那个request不是同⼀个对象所以传递参数,⾃然就获取不到了下⾯我们换思路,另辟蹊径,我们⽤session session的⽣命周期长啊 所以完全可以获取⾄于session与request的知识,请查阅相关资料下⾯贴出登录出现密码或⽤户名错误 跳转⾄login页⾯<%userBean bean = new userBean();String name = request.getParameter("userName");String password = request.getParameter("userPassword");String str1 = "";if(bean.isVerifyLoginInfo(name, password))str1 = "login success";else{str1 = "fail in login,password or username has error.";%><%-- 请求转发<jsp:forward page="login.jsp"><jsp:param name="error" value="<%=str1%>"/></jsp:forward>--%><%//请求重定向session.setAttribute("error",str1);//request.setAttribute("error",str1);response.sendRedirect("login.jsp");}%> <% //利⽤重定向获取到的参数属性//String str = (String)session.getAttribute("error");String str = (String)request.getAttribute("error");/*利⽤请求转发获取的参数String str = request.getParameter("error");if(str == null)str="";*/if(str == null)str="";%><h2 align="center"><font color="red"><%=str %></font></h2> userBean对⽤户检测部分进⾏了封装。
springboot——重定向解决刷新浏览器造成表单重复提交的问题(超详细)原因:造成表单重复提交的原因是当我们刷新浏览器的时候,浏览器会发送上⼀次提交的请求。
由于上⼀次提交的请求⽅式为post,刷新浏览器就会重新发送这个post请求,造成表单重复提交。
解决办法:将请求当前页⾯的⽅式由请求转发改为重定向到当前页⾯即可。
举例:编写⼀个处理登录请求的controller,登录成功就转到dashboard.html,登录失败则跳转到登录页⾯login.html重新登录。
注:dashboard.html和login.htm都是templates包下的。
@Controllerpublic class LoginController {@PostMapping("/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Map<String,Object> map){if (StringUtils.isEmpty(username) && "123456".equals(password)){return "dashboard";}else {map.put("msg","⽤户名或密码错误");return "login";}}}上边这段代码是不正确的,会造成表单重复提交。
当我们输⼊正确账号密码时就会return "dashboard";就会经过视图解析器转发到了dashboard.html页⾯,这样当我们浏览器中刷新是就会造成重复提交。
SpringBoot项⽬中遇到的BUG问题及解决⽅法1.启动项⽬的时候报错1.Error starting ApplicationContext.To display the auto-configuration report re-run your application with 'debug' enabled.解决⽅法:在yml配置⽂件中加⼊debug: true,因为默认的话是false2.在集成mybatis时mapper包中的类没被扫描org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'erMapper' available:expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}解决⽅法:在springboot的启动类中加⼊@MapperScan("mapper类的路径")或者直接在Mapper类上⾯添加注解@Mapper,建议使⽤上⾯那种,不然每个mapper加个注解也挺⿇烦的3.在向数据库插⼊数据时报错"\r\n### Error updating database. Cause:com.mysql.jdbc.MysqlDataTruncation:Data truncation: Data too long for column 'password' at row 1\r\n###数据库表password这个字段太短了,应该设长点ng.ClassCastException:er cannot be cast to ng.Integer4.⽤mybatis查询时报错org.mybatis.spring.MyBatisSystemException:nested exception is org.apache.ibatis.binding.BindingException:Parameter 'user_type' not found. Available parameters are [2, 1, 0, param1, param2, param3]原因:@Param注解缺失,当只有⼀个参数时,Mapper接⼝中可以不使⽤public User getUser(String name);有多个参数时就必须使⽤public User getUser(@Param("name") String name,@Param("password") String password);5.Mybatis查询传⼊⼀个字符串传参数报错mapper接⼝:PkRecord findByPkStudentNumber(String pkStudentNumber);对应的mapper配置⽂件<select id="findByPkStudentNumber" resultMap="recordMap" >SELECT * FROM pk_record<where><if test="pkStudentNumber!=null">pk_student_number=#{pkStudentNumber}</if></where></select>然后就会报如下错误There is no getter for property named 'XXX' in 'class ng.String'原因:Mybatis默认采⽤ONGL解析参数,所以会⾃动采⽤对象树的形式取string.num值,引起报错。
22.SpringBoot实现登录功能(转发和重定向)和拦截器功能【前后端结合主要学思想和使⽤】这⾥不连接数据库直接通过控制器判断和 SpringMVC 拓展实现:学习⽬标:1 . 转发和重定向,这⾥会详细说明;2. 拦截器的定义和注册3. thymeleaf 的⼀些绑定(略将其实直接去取数据即可)最后的项⽬结构:1. 登录功能的实现:package com.bihu.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpSession;import java.util.Map;@Controllerpublic class userController {/***⾸页登录控制器* @return然后视图直接访问*/@RequestMapping(value = {"/","/index"})public String login(){return "index";}/**** 登录控制器* @param username ⽤户名* @param password 密码* @param message 登录失败分发的消息* @param session Session对象主要⽤来传⽤户名给拦截器* @return*/@PostMapping("/login")public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map<String,Object> message, HttpSession session){//判断登录条件【这⾥不连接数据库】if((username!=null && username.equals("Admin")) && (password != null && password.equals("123456")) ){session.setAttribute("loginUsername",username); //设置⽤户名表⽰登录成功拦截器需要return "redirect:/main.html"; //防⽌重复提交但是注意这⾥重定向不能是url,这个main.html不存在因为我们的视图在template⽬录下,然⽽我们在拓展哪⾥增加了映射跳转}//登录失败那就到登录页⾯session.removeAttribute("loginUsername"); //防⽌BUG 移除⽤户名message.put("msg","抱歉,请检查您的账号或密码错误!");return "index";}}Usercontroller⾥⾯都有注释,但是为什么重定向是那样的?因为重定向是直接访问url实现,但我们的视图不是在静态⽂件夹下的我们的视图是在 template模板⽂件夹下⾯,不能直接xxxx.html 访问的,只能通过返回视图实现访问所以我们需要返回main.html 这个是不存在的但为什么会跳转因为我们配置类中拓展了 SpringMVC的功能:所以 SpringBoot的重定向功能你懂了没转发是可以直接转的,但是转发也是不能在 template ⽂件夹下找视图⽂件,所以你可以这样:所以这就是转发和重定向,这就是重定向访问不在静态⽂件夹下⾯的的视图⽂件但是直接⽤那就是另⼀种⽤法了这⾥重点介绍前者。
springboot如何重定向携带数据RedirectAttributes ⽬录当controller层需要重定向到指定页⾯时,如何携带数据?RedirectAttributes的使⽤RedirectAttributes存值后读取不到当controller层需要重定向到指定页⾯时,如何携带数据?传统使⽤session使⽤RedirectAttributes. (利⽤session原理)优点:提供了addFlashAttribute 等⽅法.确保数据只能被使⽤⼀次后删除RedirectAttributes的使⽤public interface RedirectAttributes extends Model {RedirectAttributes addAttribute(String var1, @Nullable Object var2);RedirectAttributes addAttribute(Object var1);RedirectAttributes addAllAttributes(Collection<?> var1);RedirectAttributes mergeAttributes(Map<String, ?> var1);RedirectAttributes addFlashAttribute(String var1, @Nullable Object var2);RedirectAttributes addFlashAttribute(Object var1);Map<String, ?> getFlashAttributes();}直接在Controller的参数中添加RedirectAttributes.addFlashAttribute会在重定向到下⼀个页⾯取出这个数据以后,将session⾥⾯的数据删除\addFlashAttribute ⽅法会将数据存储在session中,访问⼀次后失效@PostMapping("/regist")public String register(RedirectAttributes attribdatautes){int data = 1;attributes.addFlashAttribute("data",data);return "redirect:/reg.html";}addAttribute ⽅法会将数据拼接在url后(get的形式)@GetMapping("/addToCartSuccess.html")public String addToCartSuccessPagez(@RequestParam("skuId") Long skuId,Model model){CartItem cartItem = cartService.selectCartItemInfo(skuId);model.addAttribute("item",cartItem);return "success";}RedirectAttributes存值后读取不到⾸先,检查Controller上⾯是@Controller还是@RestController(两者区别⾃⾏百度)其次,如下@GetMapping("/redirect")public String redirect(RedirectAttributes redirectAttributes){redirectAttributes.addFlashAttribute("test", 1);return "redirect:/show";}@GetMapping("/show")@ResponseBody//必须要添加@ModelAttribute标签,否侧将读不到值//且必须指定变量名,并不会⾃动做匹配public Map<String, Object> show(@ModelAttribute("test") int test){Map<String, Object> modelMap = new HashMap<>();modelMap.put("String", test);return modelMap;}以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
亲测SpringBoot参数传递及@RequestBody注解---踩过的坑及解决⽬录SpringBoot参数传递及@RequestBody注解注意点前台正确的js书写格式是后台正确的Controller书写格式是RequestBody作为参数使⽤为了证实这个想法,⾃⼰书写⼀个请求类⼩结⼀下SpringBoot 参数传递及@RequestBody注解注意点前台正确的js书写格式是//点击查询, 执⾏下⾯这个函数$("#searchByCriteria").click(function () {//var paramdata = $("#bck_qry_criteria_form").serializeJson();//serializeJson()是⾃定义的form 格式化为json函数var paramdata = {type:$("#bck_type").val(),title:$("#bck_title").val(),start:$("#bck_start").val(),end:$("#bck_end").val()};paramdata = JSON.stringify(paramdata);$.ajax({type: "post",data: paramdata,url: "/getNews",cache:false,headers : {"Content-Type" : "application/json;charset=utf-8"},success: function (data) {var code = data.code;var t = data.t;if (code == 200){alert(JSON.stringify("提交成功! 我们会尽快联系您!"));}if (code == 500){alert(JSON.stringify(t));}//$("#input_phone").val("");//清空输⼊框}})});上⾯中传递的参数⼀定要⽤JSON.stringify(paramdata); ⽅法将参数转换成json格式的字符串; 因为SpringBoot 中@RequestBody注解要求的必须是json格式的字符串才能注⼊参数, 第⼆就是⼤坑, ⼤坑, ⼤坑, 请求中必须带上请求头,不然会报下⾯错误org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported后台正确的Controller书写格式是其中consumes = “application/json”, 规范的讲是要加的, 它规范了,前台要传递json格式的参数. 但是如果你不加也是可以的, 亲测不加也能封装到参数.NewsParamsMap中属性@PostMapping(value = "/getNews", consumes = "application/json")@ResponseBodypublic PageInfo<NewsList> getNewsList(@RequestBody NewsParamsMap map){System.out.println("这是正确的⽤法");return null;}NewsParamsMap 中有Integer 有Date, 前台传过来都是字符串, springboot,会根据名称⼀⼀对应, 将数据转换成相应的类型.public class NewsParamsMap {private Integer type;private String title;private Date start;private Date end;...set get ⽅法...}RequestBody 作为参数使⽤最近在接收⼀个要离职同事的⼯作,接⼿的项⽬是⽤SpringBoot搭建的,其中看到了这样的写法:在CODE上查看代码⽚派⽣到我的代码⽚@RequestMapping("doThis")public String doThis(HttpServletRequest request,@RequestParam("id") Long id, // ⽤户ID@RequestParam("back_url") String back_url, // 回调地址@RequestBody TestEntity json_data // json数据,对于java实体类){//...这个是⼀个请求映射⽅法,然后⽤浏览器输⼊url:在这个⽅法中,使⽤@RequestParam获取参数,然后使⽤@RequestBody对json格式的参数转换为Java类型在运⾏的时候发现报错:Required request body is missing@RequestBody的使⽤需要加载MappingJackson2HttpMessageConverter,但是SpringBoot的官⽅⽂档提到,这个是默认已经加载的了,⽽且json字符串和javabean也没有书写的错误因此考虑到应该是请求Content-Type的问题,因为使⽤浏览器输⼊url的⽅式没有办法定义Content-Type,因此spring⽆法发现request body为了证实这个想法,⾃⼰书写⼀个请求类在CODE上查看代码⽚派⽣到我的代码⽚String add_url = "http://127.0.0.1:8080/test/doThis";URL url = new URL(add_url);HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setDoInput(true);connection.setDoOutput(true);connection.setRequestMethod("POST");connection.setUseCaches(false);connection.setInstanceFollowRedirects(true);connection.setRequestProperty("Content-Type","application/json");connection.connect();DataOutputStream out = new DataOutputStream(connection.getOutputStream());JSONObject obj = new JSONObject();obj.put("code", -1002);obj.put("message", "msg");out.writeBytes(obj.toString());out.flush();out.close();请求还是失败,经过调试,发现需要去掉所有的@RequestParam注解才能成功⼩结⼀下1、@RequestBody需要把所有请求参数作为json解析,因此,不能包含key=value这样的写法在请求url中,所有的请求参数都是⼀个json2、直接通过浏览器输⼊url时,@RequestBody获取不到json对象,需要⽤java编程或者基于ajax的⽅法请求,将Content-Type设置为application/json以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
springboot常见get、post请求参数处理spring boot 常见http get ,post请求参数处理 在定义⼀个Rest接⼝时通常会利⽤GET、POST、PUT、DELETE来实现数据的增删改查;这⼏种⽅式有的需要传递参数,后台开发⼈员必须对接收到的参数进⾏参数验证来确保程序的健壮性GET⼀般⽤于查询数据,采⽤明⽂进⾏传输,⼀般⽤来获取⼀些⽆关⽤户信息的数据POST⼀般⽤于插⼊数据PUT⼀般⽤于数据更新DELETE⼀般⽤于数据删除⼀般都是进⾏逻辑删除(即:仅仅改变记录的状态,⽽并⾮真正的删除数据)@PathVaribale 获取url中的数据@RequestParam 获取请求参数的值@GetMapping 组合注解,是 @RequestMapping(method = RequestMethod.GET) 的缩写@RequestBody 利⽤⼀个对象去获取前端传过来的数据1. PathVaribale 获取url路径的数据请求URL:localhost:8080/hello/id 获取id值实现代码如下:@RestControllerpublic class HelloController {@RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){return "id:"+id+" name:"+name;}}在浏览器中输⼊地址:localhost:8080/hello/100/hello输出:id:81name:hello2. RequestParam 获取请求参数的值获取url参数值,默认⽅式,需要⽅法参数名称和url参数保持⼀致localhost:8080/hello?id=1000@RestControllerpublic class HelloController {@RequestMapping(value="/hello",method= RequestMethod.GET)public String sayHello(@RequestParam Integer id){return "id:"+id;}}输出:id:100url中有多个参数时,如:localhost:8080/hello?id=98&&name=helloworld具体代码如下:@RestControllerpublic class HelloController {@RequestMapping(value="/hello",method= RequestMethod.GET)public String sayHello(@RequestParam Integer id,@RequestParam String name){return "id:"+id+ " name:"+name;}}获取url参数值,执⾏参数名称⽅式localhost:8080/hello?userId=1000@RestControllerpublic class HelloController {@RequestMapping(value="/hello",method= RequestMethod.GET)public String sayHello(@RequestParam("userId") Integer id){return "id:"+id;}}输出:id:100注意不输⼊id的具体值,此时返回的结果为null。
springboot项⽬拦截器重定向循环问题的解决⽬录
springboot项⽬拦截器重定向循环
解决办法
springboot拦截器⽆限循环报错
springboot项⽬拦截器重定向循环
本菜鸟很久没写东西了,这回是解决了⼀个⼩问题,希望能帮助到你。
最近写了⼀个项⽬,项⽬中写了登录拦截器,session过期的⽤户请求会重定向到登录页⾯。
写完测试发现session过期后浏览器⼀直提⽰重定向次数过多。
打开浏览器F12看到⼀直都在访问/login/login/login之类的⽆限循环路径。
后来经过⽹上查询发现我之前设置重定向地址时直接设置的"login",
这是相对路径的写法,所以每次重定向都会将"login"添加到本次请求url的后⾯,下⼀次请求还是会被拦下来。
解决办法
将"login"改为"/login",在前⾯加"/",这样就变成了绝对路径,每次重定向的时候就能正确访问了。
springboot拦截器⽆限循环报错
启动项⽬⽆限报错
这种⼀般是你的拦截器没有排除掉让那些路径通过,然⽽你⼜拦截了所以请求
这就导致⽆限重定向
还有就是符号不要写错
这样也是会⽆限报错!!
这是本⼈踩到的坑,记录⼀下!以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
Springboot路径参数是使用Springboot框架开发Web应用时常用的一种参数传参方式,它可以让开发者在URL中添加参数,从而实现更丰富的功能。
首先,我们需要了解Springboot的路径参数的基本概念。
路径参数其实就是在URL中添加的参数,它以“/”开头,也就是所谓的“路径”参数。
它可以用来实现URL参数传递,从而实现更复杂的功能。
其次,我们来看一下Springboot路径参数的实现方法。
Springboot路径参数的实现方法其实很简单,只需要在Controller的RequestMapping注解中添加@PathVariable注解即可。
@PathVariable注解可以指定一个变量,用来接收URL中的参数,也就是路径参数。
接着,Springboot路径参数还可以让开发者自定义参数的名称,这样的话,开发者可以更加灵活的使用URL参数。
@PathVariable注解支持两种形式的参数,一种是直接指定参数的名称,另一种则是使用“{参数名}”的形式指定参数名称,这样就可以实现自定义参数名称的功能。
此外,Springboot路径参数还支持传递多个参数,比如在一个URL中可以传递多个路径参数,只需要在@PathVariable注解中指定多个参数即可。
最后,Springboot路径参数还支持正则表达式,开发者可以使用正则表达式来对路径参数进行更复杂的匹配,从而实现更丰富的功能。
总之,Springboot路径参数是一种开发Web应用时常用的参数传参方式,它可以让开发者在URL中添加参数,从而实现更丰富的功能,是Web开发过程中一个不可或缺的工具。
springboot重定向传递参数
在Spring Boot中,重定向和传递参数是很常见的需求。
Spring Boot提供了多种方式来实现重定向并传递参数的功能。
一种常见的重定向和传递参数的方式是使用RedirectAttributes。
RedirectAttributes是Spring MVC提供的一种特殊的Model对象,它可以在重定向的过程中传递参数。
使用RedirectAttributes可以将参数添加到重定向的URL中,也可以将参数作为FlashAttribute传递。
首先,我们需要在处理重定向的方法中使用RedirectAttributes参数。
例如,我们有一个处理POST请求的方法,该方法在处理完请求后需要重定向到另一个页面,并传递参数:
```java
public String submitForm(Model model, RedirectAttributes redirectAttributes)
//处理表单提交的数据
//...
//添加参数到重定向URL
redirectAttributes.addAttribute("message", "成功提交表单");
//重定向到另一个页面
return "redirect:/result";
```
在上面的例子中,我们使用了addAttribute方法将参数"message"和
它的值"成功提交表单"添加到重定向的URL中。
注意,这里的
addAttribute方法是将参数添加到URL的查询字符串中。
```java
model.addAttribute("message", message);
return "result";
```
除了使用addAttribute方法将参数添加到URL的查询字符串中,我
们还可以使用addFlashAttribute方法将参数作为FlashAttribute传递。
FlashAttribute是一种特殊的Model属性,它只会在重定向的目标页面
中被保留一次。
使用FlashAttribute可以避免将敏感信息暴露在URL中。
以下是使用addFlashAttribute方法将参数作为FlashAttribute传
递的示例:
```java
public String submitForm(Model model, RedirectAttributes redirectAttributes)
//处理表单提交的数据
//...
// 添加参数到FlashAttribute
redirectAttributes.addFlashAttribute("message", "成功提交表单");
//重定向到另一个页面
return "redirect:/result";
```
在处理重定向的目标页面的方法中,我们可以直接从Model中获取FlashAttribute的值,例如:
```java
public String showResultPage(Model model)
String message = (String) model.getAttribute("message");
model.addAttribute("message", message);
return "result";
```
在这个例子中,我们首先从Model中获取FlashAttribute的值,并将其添加到目标页面的Model中,以供页面渲染使用。
除了使用RedirectAttributes,还有其他一些方法可以实现重定向并传递参数的功能。
例如,我们可以使用HttpServletRequest的setAttribute方法将参数添加到请求中,并在重定向的目标页面中使用HttpServletRequest的getAttribute方法来获取参数的值。
另外,我们还可以使用Session来传递参数。
总结起来,Spring Boot提供了多种方式来实现重定向并传递参数的功能,其中最常用的方式是使用RedirectAttributes。
我们可以使用addAttribute方法将参数添加到URL的查询字符串中,也可以使用
addFlashAttribute方法将参数作为FlashAttribute传递。
另外,我们还可以使用HttpServletRequest和Session来传递参数。
根据不同的需求,选择合适的方式来实现重定向并传递参数。