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

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

Introduction

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

Prototype

public String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException 

Source Link

Document

Retrieve the given MessageSourceResolvable (e.g.

Usage

From source file:pl.chilldev.facelets.taglib.spring.web.form.ErrorsTag.java

/**
 * {@inheritDoc}//from  w  ww  . j  a va 2  s .co  m
 *
 * @since 0.0.1
 */
@Override
public void apply(FaceletContext context, UIComponent parent) throws IOException {
    RequestContext requestContext = this.getRequestContext(context);

    // unsupported execution context
    if (requestContext == null) {
        return;
    }

    List<String> errorMessages = new ArrayList<>();
    try {
        String beanName;
        String path = this.path.getValue(context);

        // split property path
        int position = path.indexOf('.');
        if (position == -1) {
            beanName = path;
            path = null;
        } else {
            beanName = path.substring(0, position);
            path = path.substring(position + 1);
        }

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

        // nothing was wrong
        if (errors == null) {
            return;
        }

        // find error objects
        List<? extends ObjectError> objectErrors;
        if (path != null) {
            if ("*".equals(path)) {
                objectErrors = errors.getAllErrors();
            } else {
                objectErrors = errors.getFieldErrors(path);
            }
        } else {
            objectErrors = errors.getGlobalErrors();
        }

        // build messages
        for (ObjectError error : objectErrors) {
            errorMessages.add(requestContext.getMessage(error, false));
        }
    } catch (IllegalStateException error) {
        return;
    }

    String var = this.var.getValue(context);
    Object value = context.getAttribute(var);

    try {
        // invoke tag content for every error message
        for (String message : errorMessages) {
            context.setAttribute(var, message);
            this.nextHandler.apply(context, parent);
        }
    } finally {
        // recover old values
        context.setAttribute(var, value);
    }
}