Example usage for org.springframework.validation BindingResult MODEL_KEY_PREFIX

List of usage examples for org.springframework.validation BindingResult MODEL_KEY_PREFIX

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult MODEL_KEY_PREFIX.

Prototype

String MODEL_KEY_PREFIX

To view the source code for org.springframework.validation BindingResult MODEL_KEY_PREFIX.

Click Source Link

Document

Prefix for the name of the BindingResult instance in a model, followed by the object name.

Usage

From source file:jetx.ext.internal.FunctionUtils.java

/**
 * @since 1.0.7// w  w w . j  a v a2  s.  c  o  m
 */
public static Errors findErrors(JetContext jetContext) {
    Set<String> keyset = jetContext.keySet();

    Errors result = null;
    for (String key : keyset) {
        if (key.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
            try {
                result = (Errors) jetContext.get(key);
            } catch (ClassCastException e) {
                // 
            }
            if (result != null)
                return null; // Errors
        }
    }

    // ?JetContext
    JetContext parent = jetContext.getParent();
    return parent != null ? findErrors(parent) : null;
}

From source file:com.mitchellbosecke.pebble.spring.extension.function.bindingresult.BaseBindingResultFunction.java

protected BindingResult getBindingResult(String formName, EvaluationContext context) {
    String attribute = BindingResult.MODEL_KEY_PREFIX + formName;
    return (BindingResult) context.getScopeChain().get(attribute);
}

From source file:org.jboss.arquillian.warp.extension.spring.container.provider.ErrorsProviderTestCase.java

/**
 * <p>Sets up the test environment.</p>
 *
 * @throws Exception if any error occurs
 *///from  ww  w .  j  a  v  a 2s  .  c o m
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {

    initProvider(new ErrorsProvider());

    ModelMap modelMap = new ModelMap();
    modelMap.put(BindingResult.MODEL_KEY_PREFIX + "test", mock(BindingResult.class));
    when(modelAndView.getModel()).thenReturn(modelMap);
}

From source file:org.gvnix.web.json.Jackson2ServletRequestDataBinderFactory.java

/**
 * Look current Thread for {@link ServletRequestDataBinder} created by
 * {@link DataBinderMappingJackson2HttpMessageConverter}, if found return
 * it, otherwise it delegates on parent method.
 * /*  www .  j  av  a  2s.c  o  m*/
 * @param target
 * @param objectName
 * @param request
 * @return ServletRequestDataBinder
 */
@Override
protected ServletRequestDataBinder createBinderInstance(Object target, String objectName,
        NativeWebRequest request) {
    try {
        ServletRequestDataBinder binder = (ServletRequestDataBinder) ThreadLocalUtil
                .getThreadVariable(BindingResult.MODEL_KEY_PREFIX.concat("JSON_DataBinder"));
        if (binder != null) {
            return binder;
        }
        return super.createBinderInstance(target, objectName, request);
    } finally {
        ThreadLocalUtil.destroy();
    }
}

From source file:de.iew.web.isc.spring.IscRequestMethodArgumentResolver.java

public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
        NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
    DSRequest dsRequest = new DSRequest();
    WebDataBinder binder = webDataBinderFactory.createBinder(nativeWebRequest, dsRequest, "dsRequest");
    binder.setFieldMarkerPrefix("__");

    // Man kann noch eigene Property Editoren konfigurieren
    //binder.registerCustomEditor();

    if (binder instanceof ServletRequestDataBinder) {
        /*/*w  ww . j  a v  a2  s .com*/
        @TODO
                
        Wir machen es uns etwas leicht. Unser DSRequest Objekt muss
        eigentlich nur die isc_-Eigenschaften kennen. Dort ist auch isc_metaDataPreifx
        konfiguriert. Anhand dieses Prefix mssen wir die anderen Eiegenschaften auflsen.
         */
        ServletRequestDataBinder servletRequestDataBinder = (ServletRequestDataBinder) binder;
        HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();

        servletRequestDataBinder.bind(request);

        BindingResult bindingResult = binder.getBindingResult();
        modelAndViewContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + "dsRequest", bindingResult);

        return dsRequest;
    } else {
        throw new UnsupportedOperationException(
                "Using " + binder.getClass() + " WebDataBinder is not supported.");
    }

}

From source file:de.iew.web.controllers.IdentityController.java

@RequestMapping(value = "/loginerror.html")
public ModelAndView loginError(HttpServletRequest request) {
    LoginForm loginForm = createLoginForm();

    DataBinder binder = new DataBinder(loginForm);
    MutablePropertyValues mutablePropertyValues = new MutablePropertyValues(request.getParameterMap());
    binder.bind(mutablePropertyValues);//ww w  .j  a v  a2s.c om

    ModelAndView mav = new ModelAndView(this.viewScript);
    mav.addObject(this.loginFormModelKey, loginForm);
    mav.addObject("error", true);
    mav.addObject("loginErrorMessage", "errors.login.denied.message");

    // Hole den Login Fehler und verffentliche die Fehlermeldungen
    Exception e = (Exception) request.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
    if (e instanceof ValidatingFormAuthenticationFilter.LoginFormValidationException) {
        ValidatingFormAuthenticationFilter.LoginFormValidationException ex = (ValidatingFormAuthenticationFilter.LoginFormValidationException) e;
        BindingResult res = ex.getErrors();
        /*
        Oh man :-D
                
        @see http://stackoverflow.com/questions/6704478/access-spring-mvc-bindingresult-from-within-a-view
         */
        mav.addObject(BindingResult.MODEL_KEY_PREFIX + this.loginFormModelKey, res);
    }

    return mav;
}

From source file:nl.flotsam.calendar.web.CalendarController.java

private ModelAndView getCalendar(String key, String viewName, HttpServletRequest request)
        throws NoSuchRequestHandlingMethodException {
    Calendar calendar = repository.getCalendar(key);
    if (calendar == null) {
        throw new NoSuchRequestHandlingMethodException(request);
    }//from   www . j a v  a 2 s.  co  m
    return new ModelAndView(viewName, BindingResult.MODEL_KEY_PREFIX + "calendar", calendar);
}

From source file:org.jboss.arquillian.warp.extension.spring.container.SpringMvcResultImpl.java

/**
 * <p>Retrieves the {@link BindingResult} from the model for the given attribute.</p>
 *
 * @return the {@link BindingResult}/*from w  w  w.  j a v  a 2s. c om*/
 */
private BindingResult getBindingResult(String attributeName) {

    if (modelAndView != null) {
        return (BindingResult) modelAndView.getModel().get(BindingResult.MODEL_KEY_PREFIX + attributeName);
    }

    return null;
}

From source file:org.gvnix.web.json.DataBinderDeserializer.java

/**
 * Deserializes JSON content into Map<String, String> format and then uses a
 * Spring {@link DataBinder} to bind the data from JSON message to JavaBean
 * objects.//from   w ww.j  a v a  2  s .com
 * <p/>
 * It is a workaround for issue
 * https://jira.springsource.org/browse/SPR-6731 that should be removed from
 * next gvNIX releases when that issue will be resolved.
 * 
 * @param parser Parsed used for reading JSON content
 * @param ctxt Context that can be used to access information about this
 *        deserialization activity.
 * 
 * @return Deserializer value
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object deserialize(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = parser.getCurrentToken();
    MutablePropertyValues propertyValues = new MutablePropertyValues();

    // Get target from DataBinder from local thread. If its a bean
    // collection
    // prepares array index for property names. Otherwise continue.
    DataBinder binder = (DataBinder) ThreadLocalUtil
            .getThreadVariable(BindingResult.MODEL_KEY_PREFIX.concat("JSON_DataBinder"));
    Object target = binder.getTarget();

    // For DstaBinderList instances, contentTarget contains the final bean
    // for binding. DataBinderList is just a simple wrapper to deserialize
    // bean wrapper using DataBinder
    Object contentTarget = null;

    if (t == JsonToken.START_OBJECT) {
        String prefix = null;
        if (target instanceof DataBinderList) {
            prefix = binder.getObjectName().concat("[").concat(Integer.toString(((Collection) target).size()))
                    .concat("].");

            // BeanWrapperImpl cannot create new instances if generics
            // don't specify content class, so do it by hand
            contentTarget = BeanUtils.instantiateClass(((DataBinderList) target).getContentClass());
            ((Collection) target).add(contentTarget);
        } else if (target instanceof Map) {
            // TODO
            LOGGER.warn("Map deserialization not implemented yet!");
        }
        Map<String, String> obj = readObject(parser, ctxt, prefix);
        propertyValues.addPropertyValues(obj);
    } else {
        LOGGER.warn("Deserialization for non-object not implemented yet!");
        return null; // TODO?
    }

    // bind to the target object
    binder.bind(propertyValues);

    // Note there is no need to validate the target object because
    // RequestResponseBodyMethodProcessor.resolveArgument() does it on top
    // of including BindingResult as Model attribute

    // For DAtaBinderList the contentTarget contains the final bean to
    // make the binding, so we must return it
    if (contentTarget != null) {
        return contentTarget;
    }
    return binder.getTarget();
}

From source file:com.mitchellbosecke.pebble.spring.PebbleViewResolverTest.java

@Test
public void bindingResultTestOK() throws Exception {
    Map<String, Object> model = new HashMap<>();

    model.put(BindingResult.MODEL_KEY_PREFIX + FORM_NAME, this.bindingResult);

    String result = this.render("bindingResultTest", model);
    this.assertOutput(result, EXPECTED_RESPONSE_PATH + "/bindingResultTest.html");
}