Example usage for org.springframework.web.servlet.support RequestContext getRequest

List of usage examples for org.springframework.web.servlet.support RequestContext getRequest

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support RequestContext getRequest.

Prototype

protected final HttpServletRequest getRequest() 

Source Link

Document

Return the underlying HttpServletRequest.

Usage

From source file:org.springframework.web.servlet.support.BindStatus.java

/**
 * Create a new BindStatus instance, representing a field or object status.
 * @param requestContext the current RequestContext
 * @param path the bean and property path for which values and errors
 * will be resolved (e.g. "customer.address.street")
 * @param htmlEscape whether to HTML-escape error messages and string values
 * @throws IllegalStateException if no corresponding Errors object found
 *///from w ww .  j a  va  2  s  .  c o  m
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException {

    this.requestContext = requestContext;
    this.path = path;
    this.htmlEscape = htmlEscape;

    // determine name of the object and property
    String beanName = null;
    int dotPos = path.indexOf('.');
    if (dotPos == -1) {
        // property not set, only the object itself
        beanName = path;
        this.expression = null;
    } else {
        beanName = path.substring(0, dotPos);
        this.expression = path.substring(dotPos + 1);
    }

    this.errors = requestContext.getErrors(beanName, false);

    if (this.errors != null) {
        // Usual case: An Errors instance is available as request attribute.
        // Can determine error codes and messages for the given expression.
        // Can use a custom PropertyEditor, as registered by a form controller.

        List objectErrors = null;

        if (this.expression != null) {
            if ("*".equals(this.expression)) {
                objectErrors = this.errors.getAllErrors();
            } else if (this.expression.endsWith("*")) {
                objectErrors = this.errors.getFieldErrors(this.expression);
            } else {
                objectErrors = this.errors.getFieldErrors(this.expression);
                this.value = this.errors.getFieldValue(this.expression);
                if (this.errors instanceof BindException) {
                    this.editor = ((BindException) this.errors).getCustomEditor(this.expression);
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Cannot not expose custom property editor because Errors instance ["
                                + this.errors + "] is not of type BindException");
                    }
                }
                if (htmlEscape && this.value instanceof String) {
                    this.value = HtmlUtils.htmlEscape((String) this.value);
                }
            }
        } else {
            objectErrors = this.errors.getGlobalErrors();
        }

        this.errorCodes = getErrorCodes(objectErrors);
        this.errorMessages = getErrorMessages(objectErrors);
    }

    else {
        // No Errors instance available as request attribute:
        // Probably forwarded directly to a form view.
        // Let's do the best we can: extract a plain value if appropriate.

        Object target = requestContext.getRequest().getAttribute(beanName);
        if (target == null) {
            throw new IllegalStateException("Neither Errors instance nor plain target object for bean name "
                    + beanName + " available as request attribute");
        }

        if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
            BeanWrapperImpl bw = new BeanWrapperImpl(target);
            this.value = bw.getPropertyValue(this.expression);
        }

        this.errorCodes = new String[0];
        this.errorMessages = new String[0];
    }
}