app资源网站开发,WordPress构建注册页面,机械设备上哪个网站做外贸推广,做修图网站电脑配置目录
1. 使用数组传参
1.2 传递单个参数
1.3 传递多个名称相同的参数
1.3.1 关于urlencode
2. 使用集合传参 1. 使用数组传参
创建一个Spring MVC项目#xff0c;其中 .java文件内容如下#xff1a;
package com.example.demo.controller;import com.example.demo.Per…目录
1. 使用数组传参
1.2 传递单个参数
1.3 传递多个名称相同的参数
1.3.1 关于urlencode
2. 使用集合传参 1. 使用数组传参
创建一个Spring MVC项目其中 .java文件内容如下
package com.example.demo.controller;import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;RequestMapping(/Para)
RestController
public class ParaController {RequestMapping(/M6)public String m6(String[] arrayPara){return ArrayPara has received:\n Arrays.toString(arrayPara);}
}
注注意返回语句的书写如果写成 return ArrayPara has received:\n arrayPara; 返回的是一个地址需将其转为字符串进行返回
使用浏览器构造HTTP请求发送给目标地址
1.2 传递单个参数 1.3 传递多个名称相同的参数 即当客户端在同一个请求中发送了多个同名的参数浏览器会帮我们封装为一个数组
1.3.1 关于urlencode
对于1.3 中提到的在同一个请求中传递多个名称相同的参数的问题
构造该请求时除了使用连接的键值对作为query string部分进行多参数传递的方法外
还可以直接使用逗号连接多个参数值
即构造参数如下 这种方式会被成功解析是因为Chrome浏览器会进行urlencode
关于urlencode问题在本专栏关于servlet项目前端向后端传参时也曾提及文章链接如下
【JavaEE】_前端使用GET请求的queryString向后端传参-CSDN博客https://blog.csdn.net/m0_63299495/article/details/136307074不止Chromepostman也支持urlencode操作在postman上构造这种类型的请求也可以成功发送并成功收到响应
可以加上数组长度的输出语句验证urlencode操作的执行
.java文件内容如下
package com.example.demo.controller;import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;RequestMapping(/Para)
RestController
public class ParaController {RequestMapping(/M6)public String m6(String[] arrayPara){return ArrayPara has received:\n Arrays.toString(arrayPara) \nThe length is: arrayPara.length;}
}
运行启动类后使用postman构造如下请求并发送 2. 使用集合传参
以List接口为例
.java文件内容如下
package com.example.demo.controller;import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;
import java.util.List;RequestMapping(/Para)
RestController
public class ParaController {RequestMapping(/M7)public String m7(ListString listParam){return ListParam has received:\n listParam\nThe length is: listParam.size();}
}
运行启动类后使用postman构造如下请求并发送 查看错误日志 表示默认封装方式为数组而非List接口
如果需要使用List接口需要使用一个注解RequestParam
现修改.java文件如下
package com.example.demo.controller;import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;
import java.util.List;RequestMapping(/Para)
RestController
public class ParaController {RequestMapping(/M7)public String m7(RequestParam(required false) ListString listParam){return ListParam has received:\n listParam\nThe length is: listParam.size();}
}
再次运行启动类使用postman构造如下请求并发送 可见此时参数传递成功
注关于RequestParam注解在后端代码重命名时也曾使用过此部分相关原文链接如下
【JavaEE】_Spring MVC项目之使用对象传参-CSDN博客https://blog.csdn.net/m0_63299495/article/details/136488702