简体   繁体   English

Spring MVC如何转换@RequestParam值?

[英]How does Spring MVC convert @RequestParam values?

I'm new to the Spring Framework, and as a symptom, I want to keep my adoption of its Web MVC portions as simple as possible, so I'm using the annotation functions to work with Spring. 我是Spring框架的新手,作为一种症状,我希望尽可能简单地采用它的Web MVC部分,所以我使用注释函数来处理Spring。 In the past, I've used: 在过去,我用过:
int value = Integer.valueOf(request.getParameter("numberValue"))
to pull values from parameters - explicitly converting the String returned by getParameter() . 从参数中提取值 - 显式转换getParameter()返回的String。
Helpfully, I've noticed that when I use Spring's terminology: 有帮助的是,当我使用Spring的术语时,我注意到了:
@RequestParameter("numberValue") int numVal
the conversion is handled automatically. 转换是自动处理的。 This is nice, but a "black box" to me. 这很好,但对我来说是一个“黑匣子”。 I tried looking at questions on here or in the Spring documentation, but all that information deals with custom conversions (like Converter) for objects or formatting issues. 我尝试在这里或Spring文档中查看问题,但所有这些信息都涉及对象或格式问题的自定义转换(如Converter)。 All I want to know is how Spring handles primitive type conversions for @RequestParam by default. 我想知道的是Spring默认如何为@RequestParam处理原始类型转换。

I've noticed that when I use Spring's terminology: @RequestParameter("numberValue") int numVal the conversion is handled automatically. 我注意到当我使用Spring的术语时:@RequestParameter(“numberValue”)int numVal自动处理转换。

Here you are looking for Type Conversion 在这里,您正在寻找类型转换

As per spring documentation given on this 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 (eg, binding a request parameter to a field in an @ModelAttribute parameter) they're bound to. 从请求中提取的基于字符串的值(包括请求参数,路径变量,请求标头和cookie值)可能需要转换为方法参数或字段的目标类型(例如,将请求参数绑定到@ModelAttribute中的字段参数)他们受到约束。 If the target type is not String, Spring automatically converts to the appropriate type. 如果目标类型不是String,则Spring会自动转换为适当的类型。 All simple types such as int, long, Date, etc. are supported. 支持所有简单类型,如int,long,Date等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM