Spring - disable binding exceptions (for a specific property)

In a web application, I am working on using Spring 2.5.6.SEC01, I essentially have an Integer field that takes a number to determine which page it scrolls to. The requirements have changed and we no longer want to display an error message, but simply ignore user input if they enter the wrong number, for example "adfadf".

I read that you can do this through:

TypeMismatch.property = Some new error message

However, after trying this, we still get the original error message: java.lang.Integer.TypeMismatch = ...

I want to disable this message only for this property. How can i do this? I still want the binding to be done automatically, I just don't want to hear about it now.

Walter

+4
source share
3 answers

According to DefaultMessageCodesResolver

In case of code "typeMismatch", object name "user", field "age"

  • typeMismatch.user.age
  • typeMismatch.age
  • typeMismatch.int
  • typeMismatch

So you should get (I suppose your team name is called the team, and your property is called age). Adapt according to your code.

typeMismatch.command.age typeMismatch.age typeMismatch.java.lang.Integer typeMismatch 

Note Third Code

 typeMismatch.java.lang.Integer 

He will decide what you want

UPDATE

I created a class of the Person command

 public class Person implements Serializable { private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } 

And a personal controller

 public class PersonController extends SimpleFormController { public PersonController() { setCommandClass(Person.class); setValidator(new Validator() { public boolean supports(Class clazz) { return clazz.isAssignableFrom(Person.class); } public void validate(Object command, Errors errors) { rejectIfEmpty(errors, "age", "Age is required"); } }); } @Override protected ModelAndView onSubmit(Object command) throws Exception { return new ModelAndView(); } } 

Here goes my myMessages.properties (root of classpath)

 typeMismatch.command.age=typeMismatch.command.age typeMismatch.age=typeMismatch.age typeMismatch.java.lang.Integer=typeMismatch.java.lang.Integer typeMismatch=typeMismatch 

So I did the following test

 public class PersonControllerTest { private PersonController personController; private MockHttpServletRequest request; private MessageSource messageSource; @Before public void setUp() { request = new MockHttpServletRequest(); request.setMethod("POST"); personController = new PersonController(); messageSource = new ResourceBundleMessageSource(); ((ResourceBundleMessageSource) messageSource).setBasename("myMessages"); } @Test public void failureSubmission() throws Exception { /** * Ops... a bindException * * Age can not be a plain String, It must be a plain Integer */ request.addParameter("age", "not a meaningful age"); ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse()); BindingResult bindException = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command"); for (Object object : bindException.getAllErrors()) { if(object instanceof FieldError) { FieldError fieldError = (FieldError) object; assertEquals(fieldError.getField(), "age"); /** * outputs typeMismatch.command.age */ System.out.println(messageSource.getMessage((FieldError) object, null)); } } } } 

If you want a second, you must get rid of a key resource set of type Mismatch.command.age

 typeMismatch.age=typeMismatch.age typeMismatch.java.lang.Integer=typeMismatch.java.lang.Integer typeMismatch=typeMismatch 

Or write your own version of MessageCodesResolver

 public class MyCustomMessageCodesResolver implements MessageCodesResolver { private DefaultMessageCodesResolver defaultMessageCodesResolver = new DefaultMessageCodesResolver(); public String [] resolveMessageCodes(String errorCode, String objectName) { if(errorCode.equals("age")) /** * Set up your custom message right here */ return new String[] {"typeMismatch.age"}; return defaultMessageCodesResolver.resolveMessageCodes(String errorCode, String objectName); } public void String[] resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType) { if(errorCode.equals("age")) /** * Set up your custom message right here */ return new String[] {"typeMismatch.age"}; return defaultMessageCodesResolver.resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType); } } 

And configure your PersonController

 public class PersonController extends SimpleFormController { public PersonController() { setMessageCodesResolver(new MyCustomMessageCodesResolver()); setCommandClass(Person.class); setValidator(new Validator() { public boolean supports(Class clazz) { return clazz.isAssignableFrom(Person.class); } public void validate(Object command, Errors errors) { rejectIfEmpty(errors, "age", "Age is required"); } }); } 
+4
source

You can register a custom PropertyEditor for this field that will not tolerate type mismatch.

0
source

Since this is a Spring MVC application and is assumed to be a simple form, you can install it in many different ways. Can you specify your controller settings? For a mail request, you can write down the suppressed field before the validator is called (if you specified one) or after the validator is called. If you want to do this before checking, you can call [this] [2]. After checking, you can call [this] [3]

[2]: http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html#onBind(javax.servlet.http.HttpServletRequest , java. lang.Object, org.springframework.validation.BindException) [3]: http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html# onBindAndValidate (javax.servlet.http.HttpServletRequest , java.lang.Object, org.springframework.validation.BindException)

0
source

Source: https://habr.com/ru/post/1309780/


All Articles