新闻详情

新闻详情

首页 / 资讯中心 / 详情

SpringMVC获取请求数据

发布时间:2026/9/3 6:14:00来源:尧图网络
SpringMVC获取请求数据
客户端请求参数的格式是namevaluenamevalue...服务器端要获得请求的参数有时还需要进行数据的封装SpringMVC可以接受如下类型的参数• 基本类型参数• pojo• 数组类型参数• 集合类型参数基本数据类型获取RequestMapping(value /quick11) ResponseBody//void表示不进行数据回写 responsebody 表示不尽兴页面跳转二者不冲突 public void save11(String username,int age)throws Exception{ System.out.println(username username); System.out.println(age age); }//做模拟可以在浏览器搜索框写入localhost:8080/user/quick11?usernamezhangsanage18pojo类型参数获取注意controller中的业务方法的POJO参数的属性名与请求参数的name一致参数值会自动映射匹配。package com.Itheima.domain; public class User { String name; int age; public User() { } public String getName() { return name; } public void setName(String name) { this.name name; } public int getAge() { return age; } public void setAge(int age) { this.age age; } public User(String name, int age) { this.name name; this.age age; } }RequestMapping(value /quick12) ResponseBody//pojo对象 domain包下的user对象 public void save11(User user)throws Exception{ System.out.println(user user); }//做模拟可以在浏览器搜索框写入localhost:8080/user/quick11?usernamezhangsanage18数组类型参数的获取Controller中的业务方法数组名称与请求参数的name一致参数值会自动映射匹配http://localhost:8080/user/quick12?strs111strs222strs333RequestMapping(value /quick13) ResponseBody public void quickMethod11(String[] strs)throws Exception{ System.out.println(Arrays.asList(strs));//数组打印都是地址转换成集合打印会清晰很多 }//做模拟可以在浏览器搜索框写入localhost:8080/user/quick13?strsaaastrsbbbstrsccc集合类型的参数获取情景一常见获得集合参数的时候需要将集合参数包装到pojo中才可以package com.Itheima.domain; public class User { String name; int age; public User() { } public String getName() { return name; } public void setName(String name) { this.name name; } public int getAge() { return age; } public void setAge(int age) { this.age age; } public User(String name, int age) { this.name name; this.age age; } }package com.Itheima.domain; import java.util.List; public class VO { private ListUser userList; public ListUser getUserList() { return userList; } public void setUserList(ListUser userList) {} Override public String toString() { return super.toString(); } }RequestMapping(value /quick14) ResponseBody public void quickMethod14(VO vo) throws Exception { System.out.println(vo vo); }% page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title /head body form action{pageContext.request.contextPath}/user/quick14 methodpost %-- 表明是第一个对象的name 或者age--% input typetext nameuserList[0].namebr/ input typetext nameuserList[0].agebr/ input typetext nameuserList[1].namebr/ input typetext nameuserList[1].agebr/ input typesubmit value提交 /form /body /html情景二当使用Ajax提交时可以指定contentType为json形式那么在方法参数位置使用RequestBody可以直接接受集合数据而无需使用pojo进行包装% page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title /head body script src${pageContext.request.contextPath}/js/jquery.min.js/script script let userList new Array(); userList.push({name:zhangsan,age:14}); userList.push({name:lisi,age:18}); $.post({ url:${pageContext.request.contextPath}/user/quick15, data:JSON.stringify(userList), contentType:application/json;charsetutf-8 }) /script /body /htmlRequestMapping(value /quick16) ResponseBody public void quickMethod16(RequestBody ListUser userList) throws Exception { System.out.println(userList); }因涉及静态资源访问权限必须在spring-mvc.xml添加!-- 开放资源的访问一般是静态资源 前面是到什么地方寻找 后面是寻找的位置-- mvc:resources mapping/js/** location/js// mvc:resources mapping/img/** location/img// !-- 第二种方式 如果SpringMVC找不到对应的资源就转让原始的容器这里是tomcat来找-- mvc:default-servlet-handler/防止乱码的解决方案——全局过滤!-- 配置全局过滤的fliter 防止出现乱码情况-- filter filter-nameCharacterEncodingFilter/filter-name filter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class init-param param-nameencoding/param-name param-valueUTF-8/param-value /init-param /filter filter-mapping filter-nameCharacterEncodingFilter/filter-name url-pattern/*/url-pattern /filter-mapping编码过滤器必须是 web.xml 第一个过滤器如果你在它前面放了其他 Filter、比如登录拦截、权限过滤器请求参数已经被读取解析过了再设置编码就晚了中文依然乱码。参数绑定RequestParam解决一个问题如果提交的参数和封装的参数不一致名字写错了例如name写成了username这种加上可以识别获取restful风格参数restful是一种架构风格、设计风格不是标准只提供一套设计原则和约束条件主要用于客户端和服务器交互类的软件基于这个风格设计的软件可以更加简洁更富有层次更易于实现缓存机制。restful风格的请求是使用url请求方式表示一次请求目的HTTP协议里面四个表示操作方式的动词如下GET用于获取资源POST用于新建资源PUT用于更新资源DELETE用于删除资源自定义类型转换器package com.Itheima.converter; import org.springframework.core.convert.converter.Converter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConverter implements ConverterString, Date { public Date convert(String dateString) { SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);//示例输入2020-01-01 Date date null; try { date sdf.parse(dateString); } catch (Exception e) { throw new RuntimeException(e); } return date; } }!-- 声明转换器-- bean idconversionService classorg.springframework.context.support.ConversionServiceFactoryBean property nameconverters list bean classcom.Itheima.converter.DateConverter/bean /list /property /bean mvc:annotation-driven conversion-serviceconversionService/RequestMapping(value /quick17) ResponseBody public void save17(Date date) throws Exception { System.out.println(date); }获取Servlet相关API获取请求头HTTP请求跑出请求内容请求体外还存在请求头和请求行、空行用来隔开请求头和请求体文件上传注意 enctype仅对Post方法生效例如GET方法不存在请求体当form表单修改为多部分表单时即enctypemultipart/form-data的形式request.getParameter()方法将失效。 原因本质上是获取url中 什么什么 这样形式的内容。单文件上传步骤1. 导入FileUpload和IO坐标dependency groupIdcommons-io/groupId artifactIdcommons-io/artifactId version2.4/version /dependency dependency groupIdcommons-fileupload/groupId artifactIdcommons-fileupload/artifactId version1.2.2/version /dependency2. 配置文件上传解析器!-- 配置文件上传解析器-- bean idMultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver !-- //上传文件的编码类型-- property namedefaultEncoding valueUTF-8/ !-- 上传文件的总大小-- property namemaxUploadSize value50000/ !-- 上传单个文件的大小-- property namemaxUploadSizePerFile value50000/ /bean3. 编写文件上传代码% page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title /head body form action${pageContext.request.contextPath}/user/quick22 methodpost enctype 姓名: input typetext namename 请选择文件input typefile nameuploadFile input typesubmit value提交 /form /body /htmlRequestMapping(value /quick19) ResponseBody public void quickMethod19(String name, MultipartFile uploadFile) throws Exception { System.out.println(name name); System.out.println(uploadFile uploadFile); // 获得上传文件的名称 String originalFilename uploadFile.getOriginalFilename(); uploadFile.transferTo(new File(F:\\upload\\uploadFile.getOriginalFilename())); }多文件上传% page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title /head body form action${pageContext.request.contextPath}/user/quick22 methodpost enctype 姓名: input typetext namename 请选择文件input typefile nameuploadFile 请选择需要上传的第二个文件input typefile nameuploadFile2 input typesubmit value提交 /form /body /htmlRequestMapping(value /quick19) ResponseBody public void quickMethod19(String name, MultipartFile uploadFile,MultipartFile uploadFile2) throws Exception { System.out.println(name name); System.out.println(uploadFile uploadFile); // 获得上传文件的名称 String originalFilename uploadFile.getOriginalFilename(); String originalFilename2 uploadFile2.getOriginalFilename(); uploadFile.transferTo(new File(F:\\upload\\uploadFile.getOriginalFilename())); uploadFile2.transferTo(new File(F:\\upload\\uploadFile2.getOriginalFilename())); }RequestMapping(value /quick19) ResponseBody public void quickMethod19(String name, MultipartFile[] uploadFile) throws Exception { System.out.println(name name); System.out.println(uploadFile uploadFile); for(MultipartFile multipartFile : uploadFile) { // 获得上传文件的名称 String originalFilename multipartFile.getOriginalFilename(); multipartFile.transferTo(new File(F:\\upload\\originalFilename)); } }
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

手把手教你学 Simulink——基于准 PR(比例谐振)控制的并网逆变器电流跟踪仿真(包含完整交付件) 2026/9/3 7:05:07

手把手教你学 Simulink——基于准 PR(比例谐振)控制的并网逆变器电流跟踪仿真(包含完整交付件)

目录 手把手教你学 Simulink ——基于准 PR(比例谐振)控制的并网逆变器电流跟踪仿真 一、总体系统框图 二、准 PR 控制原理 2.1 传递函数 2.2 谐波补偿(多 PR 并联) 2.3 参数设计简易法 三、关键整体参数 四、Simulink 建模 Step-by-Step Step ① —— 功率电路 …

阅读更多 →
手把手教你学Simulink--基于SPWM调制的单相双向DCAC逆变器仿真 2026/9/3 7:05:07

手把手教你学Simulink--基于SPWM调制的单相双向DCAC逆变器仿真

### 手把手教你学Simulink--基于SPWM调制的单相双向DCAC逆变器仿真 #### 摘要 本研究旨在通过对基于SPWM调制的单相双向DCAC逆变器进行Simulink仿真,验证电路设计的可行性并深入分析系统性能。在仿真过程中,利用Simulink强大的功能模块,搭建了包括直流电源、单相双向DCAC逆…

阅读更多 →
2026最新网络安全零基础入门:7天掌握渗透测试与实战技能 2026/9/3 7:05:07

2026最新网络安全零基础入门:7天掌握渗透测试与实战技能

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
技术人如何应对产业波动:从芯片供应链到技术选型的抗风险策略 2026/9/3 7:05:07

技术人如何应对产业波动:从芯片供应链到技术选型的抗风险策略

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
从零手搓FOC驱动器:三环控制参数整定与工程实践详解 2026/9/3 7:05:07

从零手搓FOC驱动器:三环控制参数整定与工程实践详解

简介:这是一份面向嵌入式电机控制开发者与高校电力电子方向学习者的FOC(磁场定向控制)驱动器实战工程,聚焦三环协同控制设计:位置环仅用P参数实现简洁跟随,速度环依据机械刚性等级调节响应特性,…

阅读更多 →
LabVIEW温度采集程序开发全解析:从硬件连接到TDMS存储 2026/9/3 7:02:07

LabVIEW温度采集程序开发全解析:从硬件连接到TDMS存储

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞