Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As said:</p> <blockquote> <p>What I want is the <strong>specific error message generated by the PropertyEditor</strong> to surface to the error message on the Spring form</p> </blockquote> <p>Behind the scenes, Spring MVC uses a BindingErrorProcessor strategy for processing missing field errors, and <strong>for translating a PropertyAccessException to a FieldError</strong>. So if you want to override default Spring MVC BindingErrorProcessor strategy, you must provide a BindingErrorProcessor strategy according to:</p> <pre><code>public class CustomBindingErrorProcessor implements DefaultBindingErrorProcessor { public void processMissingFieldError(String missingField, BindException errors) { super.processMissingFieldError(missingField, errors); } public void processPropertyAccessException(PropertyAccessException accessException, BindException errors) { if(accessException.getCause() instanceof IllegalArgumentException) errors.rejectValue(accessException.getPropertyChangeEvent().getPropertyName(), "&lt;SOME_SPECIFIC_CODE_IF_YOU_WANT&gt;", accessException.getCause().getMessage()); else defaultSpringBindingErrorProcessor.processPropertyAccessException(accessException, errors); } } </code></pre> <p>In order to test, Let's do the following</p> <pre><code>protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { binder.registerCustomEditor(SSN.class, new PropertyEditorSupport() { public String getAsText() { if(getValue() == null) return null; return ((SSN) getValue()).toString(); } public void setAsText(String value) throws IllegalArgumentException { if(StringUtils.isBlank(value)) return; boolean somethingGoesWrong = true; if(somethingGoesWrong) throw new IllegalArgumentException("Something goes wrong!"); } }); } </code></pre> <p>Now our Test class</p> <pre><code>public class PersonControllerTest { private PersonController personController; private MockHttpServletRequest request; @BeforeMethod public void setUp() { personController = new PersonController(); personController.setCommandName("command"); personController.setCommandClass(Person.class); personController.setBindingErrorProcessor(new CustomBindingErrorProcessor()); request = new MockHttpServletRequest(); request.setMethod("POST"); request.addParameter("ssn", "somethingGoesWrong"); } @Test public void done() { ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse()); BindingResult bindingResult = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command"); FieldError fieldError = bindingResult.getFieldError("ssn"); Assert.assertEquals(fieldError.getMessage(), "Something goes wrong!"); } } </code></pre> <p>regards,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload