微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring MVC如何转换@RequestParam值?

我是Spring框架的新手,作为一种症状,我希望尽可能简单地采用它的Web MVC部分,所以我使用注释函数来处理Spring.在过去,我用过:
int value = Integer.valueOf(request.getParameter(“numberValue”))从参数中提取值 – 显式转换getParameter()返回的String.有用的,我注意到当我使用Spring的术语时:@RequestParameter(“numberValue”)int numVal
转换是自动处理的.这很好,但对我来说是一个“黑匣子”.我尝试在这里或Spring文档中查看问题,但所有这些信息都涉及对象或格式问题的自定义转换(如Converter).我想知道的是Spring认如何为@RequestParam处理原始类型转换.

I’ve noticed that when I use Spring’s terminology:
@RequestParameter(“numberValue”) int numVal the conversion is handled
automatically.

在这里,您正在寻找类型转换

根据link的弹簧文档

String-based values extracted from the request including request
parameters,path variables,request headers,and cookie values may
need to be converted to the target type of the method parameter or
field (e.g.,binding a request parameter to a field in an
@modelattribute parameter) they’re bound to. If the target type is not
String,Spring automatically converts to the appropriate type. All
simple types such as int,long,Date,etc. are supported.

原文地址:https://www.jb51.cc/spring/432722.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐