@Override public WebDataBinder createBinder(NativeWebRequest webRequest, @Nullable Object target, String objectName) throws Exception { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); WebDataBinder dataBinder = new WebDataBinder(target, objectName); dataBinder.setValidator(validator); return dataBinder; } }
@Override public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); WebDataBinder dataBinder = new WebDataBinder(target, objectName); dataBinder.setValidator(validator); return dataBinder; } }
@Override public WebDataBinder createBinder(NativeWebRequest webRequest, @Nullable Object target, String objectName) throws Exception { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); WebDataBinder dataBinder = new WebDataBinder(target, objectName); dataBinder.setValidator(validator); return dataBinder; } }
@Override public WebDataBinder createBinder(NativeWebRequest webRequest, @Nullable Object target, String objectName) { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); WebDataBinder dataBinder = new WebDataBinder(target, objectName); dataBinder.setValidator(validator); return dataBinder; } }
@Before public void setup() throws Exception { paramErrors = new MethodParameter(getClass().getDeclaredMethod("handle", Errors.class), 0); bindingResult = new WebDataBinder(new Object(), "attr").getBindingResult(); webRequest = new ServletWebRequest(new MockHttpServletRequest()); }
@Test public void updateModelBindingResult() throws Exception { String commandName = "attr1"; Object command = new Object(); ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(commandName, command); WebDataBinder dataBinder = new WebDataBinder(command, commandName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(command, container.getModel().get(commandName)); String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName; assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey)); assertEquals(2, container.getModel().size()); }
@Test public void updateModelSessionAttributesSaved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(attribute, container.getModel().get(attributeName)); assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Test public void updateModelSessionAttributesRemoved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); this.attributeStore.storeAttribute(this.webRequest, attributeName, attribute); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); container.getSessionStatus().setComplete(); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(attribute, container.getModel().get(attributeName)); assertNull(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Test // SPR-12542 public void updateModelWhenRedirecting() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); String queryParam = "123"; String queryParamName = "q"; container.setRedirectModel(new ModelMap(queryParamName, queryParam)); container.setRedirectModelScenario(true); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(queryParam, container.getModel().get(queryParamName)); assertEquals(1, container.getModel().size()); assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName)); }
/** * Create a new binder instance for the given form object and request context. Can be overridden to plug in custom * DataBinder subclasses. * <p> * Default implementation creates a standard WebDataBinder, and invokes * {@link #initBinder(RequestContext, DataBinder)} and {@link #registerPropertyEditors(PropertyEditorRegistry)}. * @param context the action execution context, for accessing and setting data in "flow scope" or "request scope" * @param formObject the form object to bind onto * @return the new binder instance * @throws Exception when an unrecoverable exception occurs * @see WebDataBinder * @see #initBinder(RequestContext, DataBinder) * @see #setMessageCodesResolver(MessageCodesResolver) */ protected DataBinder createBinder(RequestContext context, Object formObject) throws Exception { DataBinder binder = new WebDataBinder(formObject, getFormObjectName()); if (getMessageCodesResolver() != null) { binder.setMessageCodesResolver(getMessageCodesResolver()); } initBinder(context, binder); registerPropertyEditors(context, binder); return binder; }
private WebDataBinder createBinding( final ITest test, final String variableName, final String bindingVariableName, final Object bindingObject, final ConversionService conversionService, final Locale locale, final Map<String,Object> variables) { final WebDataBinder dataBinder = new WebDataBinder(bindingObject, bindingVariableName); dataBinder.setConversionService(conversionService); /* * The following are thymeleaf-testing specific calls in order to allow further customizations of the binders * being created. */ final Map<String,Object> unmodifiableVariables = Collections.unmodifiableMap(variables); // We are iterating it! initBinder(variableName, bindingObject, test, dataBinder, locale, unmodifiableVariables); initBindingResult(variableName, bindingObject, test, dataBinder.getBindingResult(), locale, unmodifiableVariables); return dataBinder; }
private WebDataBinder createBinding( final ITest test, final String variableName, final String bindingVariableName, final Object bindingObject, final ConversionService conversionService, final Locale locale, final Map<String,Object> variables) { final WebDataBinder dataBinder = new WebDataBinder(bindingObject, bindingVariableName); dataBinder.setConversionService(conversionService); /* * The following are thymeleaf-testing specific calls in order to allow further customizations of the binders * being created. */ final Map<String,Object> unmodifiableVariables = Collections.unmodifiableMap(variables); // We are iterating it! initBinder(variableName, bindingObject, test, dataBinder, locale, unmodifiableVariables); initBindingResult(variableName, bindingObject, test, dataBinder.getBindingResult(), locale, unmodifiableVariables); return dataBinder; }
@Test public void test_initBinder() { WebDataBinder binder = new WebDataBinder(null); autoEnrollmentController.initBinder(binder); assertTrue(binder.getValidators().size() == 1 && binder.getValidator().equals(validator)); }
/** * Create a new binder instance for the given form object and request context. Can be overridden to plug in custom * DataBinder subclasses. * <p> * Default implementation creates a standard WebDataBinder, and invokes * {@link #initBinder(RequestContext, DataBinder)} and {@link #registerPropertyEditors(PropertyEditorRegistry)}. * @param context the action execution context, for accessing and setting data in "flow scope" or "request scope" * @param formObject the form object to bind onto * @return the new binder instance * @throws Exception when an unrecoverable exception occurs * @see WebDataBinder * @see #initBinder(RequestContext, DataBinder) * @see #setMessageCodesResolver(MessageCodesResolver) */ protected DataBinder createBinder(RequestContext context, Object formObject) throws Exception { DataBinder binder = new WebDataBinder(formObject, getFormObjectName()); if (getMessageCodesResolver() != null) { binder.setMessageCodesResolver(getMessageCodesResolver()); } initBinder(context, binder); registerPropertyEditors(context, binder); return binder; }
/** * Create a new binder instance for the given form object and request context. Can be overridden to plug in custom * DataBinder subclasses. * <p> * Default implementation creates a standard WebDataBinder, and invokes * {@link #initBinder(RequestContext, DataBinder)} and {@link #registerPropertyEditors(PropertyEditorRegistry)}. * @param context the action execution context, for accessing and setting data in "flow scope" or "request scope" * @param formObject the form object to bind onto * @return the new binder instance * @throws Exception when an unrecoverable exception occurs * @see WebDataBinder * @see #initBinder(RequestContext, DataBinder) * @see #setMessageCodesResolver(MessageCodesResolver) */ protected DataBinder createBinder(RequestContext context, Object formObject) throws Exception { DataBinder binder = new WebDataBinder(formObject, getFormObjectName()); if (getMessageCodesResolver() != null) { binder.setMessageCodesResolver(getMessageCodesResolver()); } initBinder(context, binder); registerPropertyEditors(context, binder); return binder; }
/** * Create a new binder instance for the given form object and request context. Can be overridden to plug in custom * DataBinder subclasses. * <p> * Default implementation creates a standard WebDataBinder, and invokes * {@link #initBinder(RequestContext, DataBinder)} and {@link #registerPropertyEditors(PropertyEditorRegistry)}. * @param context the action execution context, for accessing and setting data in "flow scope" or "request scope" * @param formObject the form object to bind onto * @return the new binder instance * @throws Exception when an unrecoverable exception occurs * @see WebDataBinder * @see #initBinder(RequestContext, DataBinder) * @see #setMessageCodesResolver(MessageCodesResolver) */ protected DataBinder createBinder(RequestContext context, Object formObject) throws Exception { DataBinder binder = new WebDataBinder(formObject, getFormObjectName()); if (getMessageCodesResolver() != null) { binder.setMessageCodesResolver(getMessageCodesResolver()); } initBinder(context, binder); registerPropertyEditors(context, binder); return binder; }