Kevin
Kevin

Reputation: 25269

spring mvc binding/validation

so lets say I have some model class:

public class MyRequestParams {
    private Long val = Long.valueOf(0); // default value

    // ... plus some other stuff
}

And what I want, is if the user provides http://host/path?val=& OR http://host/path?val=abc, which is invalid params, I don't want it to fail. I want it to just apply the default. I'm not sure how to do this at the moment except for storing val as a string and doing any checks to apply the default myself. Is there some easy way while keeping the type == Long?

Upvotes: 1

Views: 693

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

Not tested, but you could register a custom editor for your property, using registerCustomEditor, in a method annotated with @InitBinder. This editor would simply return the default long value.

See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html for documentation and examples.

Upvotes: 1

Related Questions