Announcement Announcement Module
Collapse
No announcement yet.
Failed to convert property value of type [java.lang.String] to required type [Integer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Failed to convert property value of type [java.lang.String] to required type [Integer

    I am getting below error for clientId property of type Integer in my MVC application:

    Code:
    Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property clientId; nested exception is java.lang.NumberFormatException: For input string: ""
    below is the initbinder code in the controller class:

    Code:
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
    
        binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, false));
    
    }
    I understand that this error is coming because i am passing spaces in the input field for clientId which is of type Integer.

    but, what is the solution. one approach is to use client side javascript validation to check the values.. but is their any better approach to resolve the issue. thank you.

  • #2
    now, the above issue is resoled by passing true instead of false in the registerCustomeEditor method of binder as follows:

    binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));

    but, giving similar error for alpha values set into the integer field.. is there a way to resolve it in the initbinder.

    Comment


    • #3
      Registering Integer

      Well, you can never reall y know what a user will post in the text field, soo instead of expecting an integer, how about accepting the string and parsing it checking for NumberFormatException?

      Comment


      • #4
        If the numeric field is to be mandatory, you should use proper Spring form validation to enforce that something is written there and that this something is effectively a number. If the field is non-mandatory, then you should check in your controller if the value entered is null, empty string or a non number and in those cases ignore it, else convert it and use the integer value as you intend to do.

        Comment

        Working...
        X