如何Spring MVC的转换@RequestParam值? [英] How does Spring MVC convert @RequestParam values?
问题描述
我是新来的Spring框架,作为一个症状,我要保持我通过其Web MVC框架的部分尽可能的简单,所以我使用的标注功能与Spring的工作。在过去,我用:结果 int值= Integer.valueOf(的request.getParameter(numberValue))
结果从参数中提取值 - 明确转换由返回的字符串。的getParameter()
结果有益的是,我注意到,当我使用Spring的术语:结果 @RequestParameter(numberValue)INT numVal
结果
转换是自动处理。这是好的,但一个黑盒子给我。我试图寻找在这里或Spring文档中的问题,而是使用自定义转换(例如转换器)的对象或格式化问题的所有信息交易。所有我想知道的是默认Spring是如何处理的 @RequestParam
基本类型转换。
我注意到,当我使用Spring的术语:
@RequestParameter(numberValue)INT numVal转换处理
自动。
块引用>在这里,您正在寻找的类型转换
根据这个<给定的春天文档href=\"http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-typeconversion\"相对=nofollow>链接
从请求包括请求中提取基于字符串的值
参数,路径变量,请求头和cookie的值可能
需要转换的方法参数的目标类型或
字段(例如,在一个一个请求参数绑定到一个场
@ModelAttribute参数)他们必然。如果目标类型不是
串,春天会自动转换为相应的类型。所有
简单的类型,如int,长,日期等的支持。
块引用>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. In the past, I've used:
int value = Integer.valueOf(request.getParameter("numberValue"))
to pull values from parameters - explicitly converting the String returned bygetParameter()
.
Helpfully, I've noticed that when I use Spring's terminology:@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. All I want to know is how Spring handles primitive type conversions for@RequestParam
by default.解决方案I've noticed that when I use Spring's terminology: @RequestParameter("numberValue") int numVal the conversion is handled automatically.
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 (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.
这篇关于如何Spring MVC的转换@RequestParam值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!