我是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.