2

My code looks like this

@RequestMapping(value = "/test2Form", method = RequestMethod.POST)
public String testForm(@ModelAttribute("someBean") SomeBean someBean, BindingResult result) {
    BindException errors = new BindException(someBean, "someBean");
    someValidator.validate(someBean, errors);

    for (FieldError error : errors.getFieldErrors()) {
        result.addError(new FieldError("someBean", error.getField(), error.getCode()));
    }

    // Field error
    if (result.hasErrors()) {
        return "test/test1";
    }

    return "index";
}

public class SomeBean{
    private Integer someInteger;
    // getter and setter
}

This works fine until the user changes the value via HTML to a non Integer value, e.g. like this: http://www.pichost.de/image/9IN

In this case a type mismatch occurs (something like Failed to convert property value of type java.lang.String to required type java.lang.Integer for property someInteger; nested exception is java.lang.NumberFormatException). I don't want to display this generated error message. Instead, I want to display only my error messages. Is this possible?

My JSP looks like this:

<form:form action="test2Form.htm" commandName="someBean" method="post">
    <form:select path="someInteger">
        <option value="1">Value 1</option>
        <option value="2">Value 2</option>
        <option value="3">Value 3</option>
    </form:select>
    <form:errors path="someInteger" element="div"/>

    <input type="submit" value="Submit">
</form:form>
8
  • What do you mean handle this properly? Spring has already handled it for you within the BindingResult. Commented Jun 17, 2015 at 14:51
  • That's true - but I don't want to display the error message generated from Spring (I want to display only the error messages I've added via someValidator). Is this possible? Commented Jun 17, 2015 at 14:53
  • You'll need to employ ExceptionHandling in your Java code to avoid these errors. Then you can offer the user your own custom error messages. Commented Jun 17, 2015 at 15:07
  • And what are you going to show? Binding didn't happen because it wasn't possible, so what are you going to show? Also it is quite a contraption you created, why are you constructing your own BindingResult? It would be easier to add @Valid to the method argument and use an @InitBinder method to set the validator, that way you save some code and use the default mechanism. Commented Jun 17, 2015 at 15:40
  • 1
    You can easily override the Failed to convert ... messages to anything you want. Take a look at this answer for details. Commented Jun 17, 2015 at 16:47

1 Answer 1

2

You can add an exception handler to you controller to handle NumberFormatExcpetion.

@ExceptionHandler(NumberFormatException.class) 
public String handleNumberFormatException(NumberFormatException e...) {
//add error for view here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.