SpringMVC请求参数绑定
绑定机制
SpringMVC绑定请求参数的过程是通过把表单提交请求参数,作为控制器中方法参数进行绑定的。
基本类型或者String
如果是基本类型或者String类型:要求我们的参数名称必须和控制器中方法的形参名称保持一致。(严格区分大小写)
补充:如果在servlet中传123,接收到是字符串,还需要自己做类型转换;但是springmvc不需要,它有内置转换器
POJO类型
如果是 POJO 类型,或者它的关联对象:要求表单中参数名称和 POJO 类的属性名称保持一致。并且控制器方法的参数类型是 POJO 类型。
使用示例
基本类型和String类型作为参数
jsp 代码:
1 2 3 4 5 6 7 8 9 10 11 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>Title</title> </head> <body> <h2> <a href= "/account/queryAccount?accountId=123456&accountName=jack" >查询账户</a> </h2> </body> </html> |
控制器代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.qzcsbj.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping( "/account" ) public class AccountController { @RequestMapping( "/queryAccount" ) public String findAccount(Integer accountId, String accountName ) { System. out .println( "查询账户id为:" +accountId + ", name为:" + accountName); return "success" ; } } |
启动tomcat
点击“查询账户”,可以看到url对参数做了拼接
日志
POJO 类型作为参数
表单数据多,可以用对象来封装
Account类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.qzcsbj.pojo; import java.io.Serializable; public class Account implements Serializable { private static final long serialVersionUID = 418607476187912306L; private Integer id; private String name; private Float money; private Address address; // 对象属性 public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Float getMoney() { return money; } public void setMoney(Float money) { this .money = money; } public Address getAddress() { return address; } public void setAddress(Address address) { this .address = address; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\ '' + ", money=" + money + ", address=" + address + '}' ; } } |
Address类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.qzcsbj.pojo; import java.io.Serializable; public class Address implements Serializable { private static final long serialVersionUID = -8420350157709769474L; private String province; private String city; public String getProvince() { return province; } public void setProvince(String province) { this .province = province; } public String getCity() { return city; } public void setCity(String city) { this .city = city; } @Override public String toString() { return "Address{" + "province='" + province + '\ '' + ", city='" + city + '\ '' + '}' ; } } |
jsp 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>Title</title> </head> <body> <div class = "form" > <form action= "/account/saveAccount" method= "post" > 账户名称:<input type= "text" name= "name" /><br/> 账户金额:<input type= "text" name= "money" /><br/> 所在省份:<input type= "text" name= "address.province" /><br/> 所在城市:<input type= "text" name= "address.city" /><br/> <input type= "submit" value= "提交" /> </form> </div> </body> </html> |
控制器代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.qzcsbj.controller; import com.qzcsbj.pojo.Account; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping( "/account" ) public class AccountController { @RequestMapping( "/queryAccount" ) public String findAccount(Integer accountId, String accountName ) { System. out .println( "查询账户id为:" +accountId + ", name为:" + accountName); return "success" ; } @RequestMapping( "/saveAccount" ) public String saveAccount(Account account) { System. out .println( "账户是:" +account); return "success" ; } } |
重启tomcat
结果
输入内容
点击“提交”后
日志
__EOF__

本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2022-01-11 k8s节点简介、移除节点、新增节点;删除k8s