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:org.springframework.boot.autoconfigure.web.BasicErrorController.java

@ExceptionHandler(Exception.class)
public void handle(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception {
    this.resolver.resolveException(request, response, null, e);
    if (response.getStatus() == HttpServletResponse.SC_OK) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }/* ww w .  j  av a2s.  c  o m*/
    // There's only one exception so it's easier for the error controller to identify
    // it this way...
    request.setAttribute(ErrorController.class.getName(), e);
    if (e instanceof BindException) {
        // ... but other error pages might be looking for it here as well
        request.setAttribute(BindingResult.MODEL_KEY_PREFIX + ((BindException) e).getObjectName(), e);
    }
}

From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java

public final void updateModelAttributes(Object handler, Map<String, Object> mavModel,
        ExtendedModelMap implicitModel, NativeWebRequest webRequest) throws Exception {

    if (this.methodResolver.hasSessionAttributes() && this.sessionStatus.isComplete()) {
        for (String attrName : this.methodResolver.getActualSessionAttributeNames()) {
            this.sessionAttributeStore.cleanupAttribute(webRequest, attrName);
        }//from   ww  w  . j  a v  a  2s.  c o  m
    }

    // Expose model attributes as session attributes, if required.
    // Expose BindingResults for all attributes, making custom editors available.
    Map<String, Object> model = (mavModel != null ? mavModel : implicitModel);
    if (model != null) {
        try {
            String[] originalAttrNames = model.keySet().toArray(new String[model.size()]);
            for (String attrName : originalAttrNames) {
                Object attrValue = model.get(attrName);
                boolean isSessionAttr = this.methodResolver.isSessionAttribute(attrName,
                        (attrValue != null ? attrValue.getClass() : null));
                if (isSessionAttr) {
                    if (this.sessionStatus.isComplete()) {
                        implicitModel.put(MODEL_KEY_PREFIX_STALE + attrName, Boolean.TRUE);
                    } else if (!implicitModel.containsKey(MODEL_KEY_PREFIX_STALE + attrName)) {
                        this.sessionAttributeStore.storeAttribute(webRequest, attrName, attrValue);
                    }
                }
                if (!attrName.startsWith(BindingResult.MODEL_KEY_PREFIX)
                        && (isSessionAttr || isBindingCandidate(attrValue))) {
                    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attrName;
                    if (mavModel != null && !model.containsKey(bindingResultKey)) {
                        WebDataBinder binder = createBinder(webRequest, attrValue, attrName);
                        initBinder(handler, attrName, binder, webRequest);
                        mavModel.put(bindingResultKey, binder.getBindingResult());
                    }
                }
            }
        } catch (InvocationTargetException ex) {
            // User-defined @InitBinder method threw an exception...
            ReflectionUtils.rethrowException(ex.getTargetException());
        }
    }
}

From source file:org.springframework.test.web.portlet.server.result.ModelResultMatchers.java

public <T> PortletResultMatcher size(final int size) {
    return new PortletResultMatcher() {
        public void match(PortletMvcResult result) throws Exception {
            Map<String, Object> model = getModel(result);
            int actual = 0;
            for (String key : model.keySet()) {
                if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
                    actual++;/*from w  w  w .  j  ava  2s  . com*/
                }
            }
            assertEquals("Model size", size, actual);
        }
    };
}

From source file:org.springframework.web.method.annotation.ModelFactory.java

/**
 * Add {@link BindingResult} attributes to the model for attributes that require it.
 *///from   w w w  . j  a  v  a2  s  .  co m
private void updateBindingResult(NativeWebRequest request, ModelMap model) throws Exception {
    List<String> keyNames = new ArrayList<>(model.keySet());
    for (String name : keyNames) {
        Object value = model.get(name);
        if (value != null && isBindingCandidate(name, value)) {
            String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
            if (!model.containsAttribute(bindingResultKey)) {
                WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name);
                model.put(bindingResultKey, dataBinder.getBindingResult());
            }
        }
    }
}

From source file:org.springframework.web.method.annotation.ModelFactory.java

/**
 * Whether the given attribute requires a {@link BindingResult} in the model.
 *//* www  .ja v  a  2  s.  c o  m*/
private boolean isBindingCandidate(String attributeName, Object value) {
    if (attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
        return false;
    }

    if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, value.getClass())) {
        return true;
    }

    return (!value.getClass().isArray() && !(value instanceof Collection) && !(value instanceof Map)
            && !BeanUtils.isSimpleValueType(value.getClass()));
}

From source file:org.springframework.webflow.mvc.view.AbstractMvcView.java

private void exposeBindingModel(Map<String, Object> model) {
    Object modelObject = getModelObject();
    if (modelObject != null) {
        BindingModel bindingModel = new BindingModel(getModelExpression().getExpressionString(), modelObject,
                expressionParser, conversionService, requestContext.getMessageContext());
        bindingModel.setBinderConfiguration(binderConfiguration);
        bindingModel.setMappingResults(mappingResults);
        model.put(BindingResult.MODEL_KEY_PREFIX + getModelExpression().getExpressionString(), bindingModel);
    }/*from   w w w .ja  v  a  2  s  .  c  o  m*/
}