- Is it possible to give an object a default value using @RequestParam?
when i name the form tag the same as the field in the object I know that it automatically assigns a value to an object. But if the object's field is an int, null value is entered, an error occurs.
★VO enter image description here
★form enter image description here
★controller enter image description here
public String reg4(HttpServletRequest request, HttpServletResponse response, Plant_list2VO plant_list2VO, @RequestParam(name="inv_count", defaultValue="0") int inv_count, @RequestParam(name="inv_count_disable", defaultValue="2") int inv_count_disable,Model model) {
}
★ console
WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors Field error in object 'plant_list2VO' on field 'inv_count': rejected value []; codes [typeMismatch.plant_list2VO.inv_count,typeMismatch.inv_count,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [plant_list2VO.inv_count,inv_count]; arguments []; default message [inv_count]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'inv_count'; nested exception is java.lang.NumberFormatException: For input string: ""] Field error in object 'plant_list2VO' on field 'inv_count_disable': rejected value []; codes [typeMismatch.plant_list2VO.inv_count_disable,typeMismatch.inv_count_disable,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [plant_list2VO.inv_count_disable,inv_count_disable]; arguments []; default message [inv_count_disable]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'inv_count_disable'; nested exception is java.lang.NumberFormatException: For input string: ""]]
★ console
CodePudding user response:
I see you define inv_count and inv_count_disable are int attributes. So, you should change values of defaultValue
to a number to resolve java.lang.NumberFormatException
CodePudding user response:
Judging by the console log, the inv_count
and inv_count_disable
values passed to your controller are not null, but empty Strings.
I can assume you use an old version of Spring, because before 3.2.x version empty String values were accepted by AbstractNamedValueMethodArgumentResolver
as actual values (see method resolveArgument
here), and defaultValue
wasn't used in that case, resulting in conversion exception, because empty String cannot be converted to an integer.
In 3.2.x and later versions of Spring empty String is considered as there were no value passed, and defaultValue
is used in case it's present (see method resolveArgument
here).
So, if you want to stick to an older version of Spring, you may want to change your form to send some default number if the field is empty, or create a DTO, where these fields are of type String
with some logic in setters.
Also this may help: Is it possible to have empty RequestParam values use the defaultValue?