List of usage examples for org.springframework.web.servlet.support RequestContext getErrors
@Nullable public Errors getErrors(String name, boolean htmlEscape)
From source file:pl.chilldev.facelets.taglib.spring.web.form.ErrorsTag.java
/** * {@inheritDoc}//from w ww . j ava 2 s.c om * * @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); } }
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 ww w . j a v a2 s. co 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]; } }