Polishing

This commit is contained in:
Juergen Hoeller 2018-02-02 11:34:03 +01:00
parent 4a1cc9ced7
commit 7f96827ade
5 changed files with 51 additions and 45 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,8 +29,8 @@ import org.springframework.lang.Nullable;
public interface MessageCodeFormatter { public interface MessageCodeFormatter {
/** /**
* Build and return a message code consisting of the given fields, usually delimited * Build and return a message code consisting of the given fields,
* by {@link DefaultMessageCodesResolver#CODE_SEPARATOR}. * usually delimited by {@link DefaultMessageCodesResolver#CODE_SEPARATOR}.
* @param errorCode e.g.: "typeMismatch" * @param errorCode e.g.: "typeMismatch"
* @param objectName e.g.: "user" * @param objectName e.g.: "user"
* @param field e.g. "age" * @param field e.g. "age"
@ -38,4 +38,5 @@ public interface MessageCodeFormatter {
* @see DefaultMessageCodesResolver.Format * @see DefaultMessageCodesResolver.Format
*/ */
String format(String errorCode, @Nullable String objectName, @Nullable String field); String format(String errorCode, @Nullable String objectName, @Nullable String field);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import org.springframework.web.util.HtmlUtils;
* tags and macros, the same status class is used for both scenarios. * tags and macros, the same status class is used for both scenarios.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 5.0 * @since 5.0
* @see RequestContext#getBindStatus * @see RequestContext#getBindStatus
*/ */
@ -58,9 +59,13 @@ public class BindStatus {
@Nullable @Nullable
private final Errors errors; private final Errors errors;
private final String[] errorCodes;
@Nullable @Nullable
private BindingResult bindingResult; private String[] errorMessages;
@Nullable
private List<? extends ObjectError> objectErrors;
@Nullable @Nullable
private Object value; private Object value;
@ -75,12 +80,7 @@ public class BindStatus {
private PropertyEditor editor; private PropertyEditor editor;
@Nullable @Nullable
private List<? extends ObjectError> objectErrors; private BindingResult bindingResult;
private String[] errorCodes = new String[0];
@Nullable
private String[] errorMessages;
/** /**
@ -91,9 +91,7 @@ public class BindStatus {
* @param htmlEscape whether to HTML-escape error messages and string values * @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found * @throws IllegalStateException if no corresponding Errors object found
*/ */
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException {
throws IllegalStateException {
this.requestContext = requestContext; this.requestContext = requestContext;
this.path = path; this.path = path;
this.htmlEscape = htmlEscape; this.htmlEscape = htmlEscape;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.web.reactive.result.view; package org.springframework.web.reactive.result.view;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -46,7 +47,9 @@ public interface View {
/** /**
* Return the list of media types this View supports, or an empty list. * Return the list of media types this View supports, or an empty list.
*/ */
List<MediaType> getSupportedMediaTypes(); default List<MediaType> getSupportedMediaTypes() {
return Collections.emptyList();
}
/** /**
* Whether this View does rendering by performing a redirect. * Whether this View does rendering by performing a redirect.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,11 +17,9 @@
package org.springframework.web.servlet; package org.springframework.web.servlet;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
@ -41,6 +39,7 @@ import org.springframework.lang.Nullable;
* *
* @author Rod Johnson * @author Rod Johnson
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Rossen Stoyanchev
* @see org.springframework.web.servlet.view.AbstractView * @see org.springframework.web.servlet.view.AbstractView
* @see org.springframework.web.servlet.view.InternalResourceView * @see org.springframework.web.servlet.view.InternalResourceView
*/ */
@ -49,6 +48,7 @@ public interface View {
/** /**
* Name of the {@link HttpServletRequest} attribute that contains the response status code. * Name of the {@link HttpServletRequest} attribute that contains the response status code.
* <p>Note: This attribute is not required to be supported by all View implementations. * <p>Note: This attribute is not required to be supported by all View implementations.
* @since 3.0
*/ */
String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus"; String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus";
@ -56,41 +56,44 @@ public interface View {
* Name of the {@link HttpServletRequest} attribute that contains a Map with path variables. * Name of the {@link HttpServletRequest} attribute that contains a Map with path variables.
* The map consists of String-based URI template variable names as keys and their corresponding * The map consists of String-based URI template variable names as keys and their corresponding
* Object-based values -- extracted from segments of the URL and type converted. * Object-based values -- extracted from segments of the URL and type converted.
*
* <p>Note: This attribute is not required to be supported by all View implementations. * <p>Note: This attribute is not required to be supported by all View implementations.
* @since 3.1
*/ */
String PATH_VARIABLES = View.class.getName() + ".pathVariables"; String PATH_VARIABLES = View.class.getName() + ".pathVariables";
/** /**
* The {@link MediaType} selected during content negotiation, which may be * The {@link org.springframework.http.MediaType} selected during content negotiation,
* more specific than the one the View is configured with. For example: * which may be more specific than the one the View is configured with. For example:
* "application/vnd.example-v1+xml" vs "application/*+xml". * "application/vnd.example-v1+xml" vs "application/*+xml".
* @since 3.2
*/ */
String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType"; String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType";
/** /**
* Return the content type of the view, if predetermined. * Return the content type of the view, if predetermined.
* <p>Can be used to check the content type upfront, * <p>Can be used to check the view's content type upfront,
* before the actual rendering process. * i.e. before an actual rendering attempt.
* @return the content type String (optionally including a character set), * @return the content type String (optionally including a character set),
* or {@code null} if not predetermined. * or {@code null} if not predetermined
*/ */
@Nullable @Nullable
String getContentType(); default String getContentType() {
return null;
}
/** /**
* Render the view given the specified model. * Render the view given the specified model.
* <p>The first step will be preparing the request: In the JSP case, * <p>The first step will be preparing the request: In the JSP case, this would mean
* this would mean setting model objects as request attributes. * setting model objects as request attributes. The second step will be the actual
* The second step will be the actual rendering of the view, * rendering of the view, for example including the JSP via a RequestDispatcher.
* for example including the JSP via a RequestDispatcher.
* @param model Map with name Strings as keys and corresponding model * @param model Map with name Strings as keys and corresponding model
* objects as values (Map can also be {@code null} in case of empty model) * objects as values (Map can also be {@code null} in case of empty model)
* @param request current HTTP request * @param request current HTTP request
* @param response HTTP response we are building * @param response HTTP response we are building
* @throws Exception if rendering failed * @throws Exception if rendering failed
*/ */
void render(@Nullable Map<String, ?> model, void render(@Nullable Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
HttpServletRequest request, HttpServletResponse response) throws Exception; throws Exception;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -61,8 +61,13 @@ public class BindStatus {
@Nullable @Nullable
private final Errors errors; private final Errors errors;
private final String[] errorCodes;
@Nullable @Nullable
private BindingResult bindingResult; private String[] errorMessages;
@Nullable
private List<? extends ObjectError> objectErrors;
@Nullable @Nullable
private Object value; private Object value;
@ -77,12 +82,7 @@ public class BindStatus {
private PropertyEditor editor; private PropertyEditor editor;
@Nullable @Nullable
private List<? extends ObjectError> objectErrors; private BindingResult bindingResult;
private String[] errorCodes = new String[0];
@Nullable
private String[] errorMessages;
/** /**
@ -141,7 +141,7 @@ public class BindStatus {
else { else {
this.objectErrors = this.errors.getGlobalErrors(); this.objectErrors = this.errors.getGlobalErrors();
} }
initErrorCodes(this.objectErrors); this.errorCodes = initErrorCodes(this.objectErrors);
} }
else { else {
@ -171,12 +171,13 @@ public class BindStatus {
/** /**
* Extract the error codes from the ObjectError list. * Extract the error codes from the ObjectError list.
*/ */
private void initErrorCodes(List<? extends ObjectError> objectErrors) { private static String[] initErrorCodes(List<? extends ObjectError> objectErrors) {
this.errorCodes = new String[objectErrors.size()]; String[] errorCodes = new String[objectErrors.size()];
for (int i = 0; i < objectErrors.size(); i++) { for (int i = 0; i < objectErrors.size(); i++) {
ObjectError error = objectErrors.get(i); ObjectError error = objectErrors.get(i);
this.errorCodes[i] = error.getCode(); errorCodes[i] = error.getCode();
} }
return errorCodes;
} }