diff --git a/spring-context/src/main/java/org/springframework/context/MessageSource.java b/spring-context/src/main/java/org/springframework/context/MessageSource.java index ead8970e6a..dbef72e882 100644 --- a/spring-context/src/main/java/org/springframework/context/MessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/MessageSource.java @@ -53,6 +53,7 @@ public interface MessageSource { * otherwise the default message passed as a parameter * @see java.text.MessageFormat */ + @Nullable String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale); /** diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java index 78883626e6..7f3f849256 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java @@ -143,8 +143,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme return msg; } if (defaultMessage == null) { - String fallback = getDefaultMessage(code); - return (fallback != null ? fallback : ""); + return getDefaultMessage(code); } return renderDefaultMessage(defaultMessage, args, locale); } @@ -264,8 +263,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme else { // Check parent MessageSource, returning null if not found there. // Covers custom MessageSource impls and DelegatingMessageSource. - String msg = parent.getMessage(code, args, null, locale); - return ("".equals(msg) ? null : msg); + return parent.getMessage(code, args, null, locale); } } // Not found in parent either. diff --git a/spring-context/src/main/java/org/springframework/context/support/DelegatingMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/DelegatingMessageSource.java index cbd370a1d0..1556e5bf1e 100644 --- a/spring-context/src/main/java/org/springframework/context/support/DelegatingMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/DelegatingMessageSource.java @@ -54,6 +54,7 @@ public class DelegatingMessageSource extends MessageSourceSupport implements Hie @Override + @Nullable public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) { if (this.parentMessageSource != null) { return this.parentMessageSource.getMessage(code, args, defaultMessage, locale); @@ -62,7 +63,7 @@ public class DelegatingMessageSource extends MessageSourceSupport implements Hie return renderDefaultMessage(defaultMessage, args, locale); } else { - return ""; + return null; } } diff --git a/spring-context/src/main/java/org/springframework/context/support/MessageSourceAccessor.java b/spring-context/src/main/java/org/springframework/context/support/MessageSourceAccessor.java index 90a584e25a..68162a1717 100644 --- a/spring-context/src/main/java/org/springframework/context/support/MessageSourceAccessor.java +++ b/spring-context/src/main/java/org/springframework/context/support/MessageSourceAccessor.java @@ -84,7 +84,8 @@ public class MessageSourceAccessor { * @return the message */ public String getMessage(String code, String defaultMessage) { - return this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale()); + String msg = this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale()); + return (msg != null ? msg : ""); } /** @@ -95,7 +96,8 @@ public class MessageSourceAccessor { * @return the message */ public String getMessage(String code, String defaultMessage, Locale locale) { - return this.messageSource.getMessage(code, null, defaultMessage, locale); + String msg = this.messageSource.getMessage(code, null, defaultMessage, locale); + return (msg != null ? msg : ""); } /** @@ -106,7 +108,8 @@ public class MessageSourceAccessor { * @return the message */ public String getMessage(String code, @Nullable Object[] args, String defaultMessage) { - return this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale()); + String msg = this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale()); + return (msg != null ? msg : ""); } /** @@ -118,7 +121,8 @@ public class MessageSourceAccessor { * @return the message */ public String getMessage(String code, @Nullable Object[] args, String defaultMessage, Locale locale) { - return this.messageSource.getMessage(code, args, defaultMessage, locale); + String msg = this.messageSource.getMessage(code, args, defaultMessage, locale); + return (msg != null ? msg : ""); } /** diff --git a/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java b/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java index 91310213c8..a42dbc56ac 100644 --- a/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java @@ -162,6 +162,7 @@ public class ResourceBundleMessageSourceTests { assertEquals("Hello, message1", ac.getMessage("hello", args, Locale.ENGLISH)); // test default message without and with args + assertNull(ac.getMessage(null, null, null, Locale.ENGLISH)); assertEquals("default", ac.getMessage(null, null, "default", Locale.ENGLISH)); assertEquals("default", ac.getMessage(null, args, "default", Locale.ENGLISH)); assertEquals("{0}, default", ac.getMessage(null, null, "{0}, default", Locale.ENGLISH)); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java index 58c528baaa..6b2616b213 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java @@ -282,6 +282,9 @@ public class RequestContext { */ public String getMessage(String code, @Nullable Object[] args, String defaultMessage, boolean htmlEscape) { String msg = this.messageSource.getMessage(code, args, defaultMessage, this.locale); + if (msg == null) { + return ""; + } return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java index db57fd4ead..aa0024b48b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java @@ -653,6 +653,9 @@ public class RequestContext { */ public String getMessage(String code, @Nullable Object[] args, String defaultMessage, boolean htmlEscape) { String msg = this.webApplicationContext.getMessage(code, args, defaultMessage, getLocale()); + if (msg == null) { + return ""; + } return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg); } @@ -732,7 +735,8 @@ public class RequestContext { * @return the message */ public String getThemeMessage(String code, String defaultMessage) { - return getTheme().getMessageSource().getMessage(code, null, defaultMessage, getLocale()); + String msg = getTheme().getMessageSource().getMessage(code, null, defaultMessage, getLocale()); + return (msg != null ? msg : ""); } /** @@ -745,7 +749,8 @@ public class RequestContext { * @return the message */ public String getThemeMessage(String code, @Nullable Object[] args, String defaultMessage) { - return getTheme().getMessageSource().getMessage(code, args, defaultMessage, getLocale()); + String msg = getTheme().getMessageSource().getMessage(code, args, defaultMessage, getLocale()); + return (msg != null ? msg : ""); } /** @@ -758,8 +763,9 @@ public class RequestContext { * @return the message */ public String getThemeMessage(String code, @Nullable List args, String defaultMessage) { - return getTheme().getMessageSource().getMessage(code, (args != null ? args.toArray() : null), + String msg = getTheme().getMessageSource().getMessage(code, (args != null ? args.toArray() : null), defaultMessage, getLocale()); + return (msg != null ? msg : ""); } /** diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/MessageTag.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/MessageTag.java index e63624bbf9..10279afe9f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/MessageTag.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/MessageTag.java @@ -300,6 +300,7 @@ public class MessageTag extends HtmlEscapingAwareTag implements ArgumentAware { this.arguments = null; } + /** * Resolve the specified message into a concrete message String. * The returned message String should be unescaped. @@ -322,8 +323,9 @@ public class MessageTag extends HtmlEscapingAwareTag implements ArgumentAware { if (this.text != null) { // We have a fallback text to consider. - return messageSource.getMessage( + String msg = messageSource.getMessage( this.code, argumentsArray, this.text, getRequestContext().getLocale()); + return (msg != null ? msg : ""); } else { // We have no fallback text to consider.