/** * Build a resolvable wrapper for the specified field, allowing to resolve the field's * name in a {@code MessageSource}. * <p>The default implementation returns a first argument indicating the field: * of type {@code DefaultMessageSourceResolvable}, with "objectName.field" and "field" * as codes, and with the plain field name as default message. * @param objectName the name of the target object * @param field the field that caused the binding error * @return a corresponding {@code MessageSourceResolvable} for the specified field * @since 4.3 */ protected MessageSourceResolvable getResolvableField(String objectName, String field) { String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; return new DefaultMessageSourceResolvable(codes, field); }
@Override public int hashCode() { int hashCode = ObjectUtils.nullSafeHashCode(getCodes()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getArguments()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getDefaultMessage()); return hashCode; }
@Override public boolean equals(@Nullable Object other) { if (this == other) { return true; } if (other == null || other.getClass() != getClass() || !super.equals(other)) { return false; } ObjectError otherError = (ObjectError) other; return getObjectName().equals(otherError.getObjectName()); }
/** * prova ad utilizzare il default message, altrimenti va sul default * * @param fieldError * @return */ private String resolveLocalizedErrorMessage(DefaultMessageSourceResolvable fieldError) { Locale currentLocale = LocaleContextHolder.getLocale(); String msgCode = StringUtils.isNotBlank(fieldError.getDefaultMessage()) ? fieldError.getDefaultMessage() : fieldError.getCode(); String localizedErrorMessage = getMessageSource().getMessage(msgCode, fieldError.getArguments(), currentLocale); return localizedErrorMessage; }
/** * Return FieldError arguments for a binding error on the given field. * Invoked for each missing required field and each type mismatch. * <p>The default implementation returns a single argument indicating the field name * (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes). * @param objectName the name of the target object * @param field the field that caused the binding error * @return the Object array that represents the FieldError arguments * @see org.springframework.validation.FieldError#getArguments * @see org.springframework.context.support.DefaultMessageSourceResolvable */ protected Object[] getArgumentsForBindError(String objectName, String field) { String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; return new Object[] {new DefaultMessageSourceResolvable(codes, field)}; }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MessageSourceResolvable)) { return false; } MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other; return (ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes()) && ObjectUtils.nullSafeEquals(getArguments(), otherResolvable.getArguments()) && ObjectUtils.nullSafeEquals(getDefaultMessage(), otherResolvable.getDefaultMessage())); }
@Override public boolean equals(@Nullable Object other) { if (this == other) { return true; } if (other == null || other.getClass() != getClass() || !super.equals(other)) { return false; } ObjectError otherError = (ObjectError) other; return getObjectName().equals(otherError.getObjectName()); }
/** * Build a resolvable wrapper for the specified field, allowing to resolve the field's * name in a {@code MessageSource}. * <p>The default implementation returns a first argument indicating the field: * of type {@code DefaultMessageSourceResolvable}, with "objectName.field" and "field" * as codes, and with the plain field name as default message. * @param objectName the name of the target object * @param field the field that caused the binding error * @return a corresponding {@code MessageSourceResolvable} for the specified field * @since 4.3 */ protected MessageSourceResolvable getResolvableField(String objectName, String field) { String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; return new DefaultMessageSourceResolvable(codes, field); }
@Override public int hashCode() { int hashCode = ObjectUtils.nullSafeHashCode(getCodes()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getArguments()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getDefaultMessage()); return hashCode; }
@Override public boolean equals(@Nullable Object other) { if (this == other) { return true; } if (other == null || other.getClass() != getClass() || !super.equals(other)) { return false; } ObjectError otherError = (ObjectError) other; return getObjectName().equals(otherError.getObjectName()); }
/** * Return FieldError arguments for a binding error on the given field. * Invoked for each missing required field and each type mismatch. * <p>The default implementation returns a single argument indicating the field name * (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes). * @param objectName the name of the target object * @param field the field that caused the binding error * @return the Object array that represents the FieldError arguments * @see org.springframework.validation.FieldError#getArguments * @see org.springframework.context.support.DefaultMessageSourceResolvable */ protected Object[] getArgumentsForBindError(String objectName, String field) { String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; return new Object[] {new DefaultMessageSourceResolvable(codes, field)}; }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MessageSourceResolvable)) { return false; } MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other; return (ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes()) && ObjectUtils.nullSafeEquals(getArguments(), otherResolvable.getArguments()) && ObjectUtils.nullSafeEquals(getDefaultMessage(), otherResolvable.getDefaultMessage())); }
@Test public void nestedMessageSourceWithParamInChild() { StaticMessageSource source = new StaticMessageSource(); StaticMessageSource parent = new StaticMessageSource(); source.setParentMessageSource(parent); source.addMessage("param", Locale.ENGLISH, "value"); parent.addMessage("with.param", Locale.ENGLISH, "put {0} here"); MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable( new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")}); assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH)); }
@Override public int hashCode() { int hashCode = ObjectUtils.nullSafeHashCode(getCodes()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getArguments()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getDefaultMessage()); return hashCode; }
@Test public void nestedMessageSourceWithParamInParent() { StaticMessageSource source = new StaticMessageSource(); StaticMessageSource parent = new StaticMessageSource(); source.setParentMessageSource(parent); parent.addMessage("param", Locale.ENGLISH, "value"); source.addMessage("with.param", Locale.ENGLISH, "put {0} here"); MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable( new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")}); assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH)); }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MessageSourceResolvable)) { return false; } MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other; return (ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes()) && ObjectUtils.nullSafeEquals(getArguments(), otherResolvable.getArguments()) && ObjectUtils.nullSafeEquals(getDefaultMessage(), otherResolvable.getDefaultMessage())); }
@Test public void messageSourceResolvable() { // first code valid String[] codes1 = new String[] {"message.format.example3", "message.format.example2"}; MessageSourceResolvable resolvable1 = new DefaultMessageSourceResolvable(codes1, null, "default"); assertTrue("correct message retrieved", MSG_TXT3_US.equals(sac.getMessage(resolvable1, Locale.US))); // only second code valid String[] codes2 = new String[] {"message.format.example99", "message.format.example2"}; MessageSourceResolvable resolvable2 = new DefaultMessageSourceResolvable(codes2, null, "default"); assertTrue("correct message retrieved", MSG_TXT2_US.equals(sac.getMessage(resolvable2, Locale.US))); // no code valid, but default given String[] codes3 = new String[] {"message.format.example99", "message.format.example98"}; MessageSourceResolvable resolvable3 = new DefaultMessageSourceResolvable(codes3, null, "default"); assertTrue("correct message retrieved", "default".equals(sac.getMessage(resolvable3, Locale.US))); // no code valid, no default String[] codes4 = new String[] {"message.format.example99", "message.format.example98"}; MessageSourceResolvable resolvable4 = new DefaultMessageSourceResolvable(codes4); exception.expect(NoSuchMessageException.class); sac.getMessage(resolvable4, Locale.US); }
@Test public void messageTagWithMessageSourceResolvable() throws JspException { PageContext pc = createPageContext(); final StringBuffer message = new StringBuffer(); MessageTag tag = new MessageTag() { @Override protected void writeMessage(String msg) { message.append(msg); } }; tag.setPageContext(pc); tag.setMessage(new DefaultMessageSourceResolvable("test")); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag()); assertEquals("Correct message", "test message", message.toString()); }
@Test @SuppressWarnings("rawtypes") public void requestContext() throws ServletException { PageContext pc = createPageContext(); RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest(), pc.getServletContext()); assertEquals("test message", rc.getMessage("test")); assertEquals("test message", rc.getMessage("test", (Object[]) null)); assertEquals("test message", rc.getMessage("test", "default")); assertEquals("test message", rc.getMessage("test", (Object[]) null, "default")); assertEquals("test arg1 message arg2", rc.getMessage("testArgs", new String[] {"arg1", "arg2"}, "default")); assertEquals("test arg1 message arg2", rc.getMessage("testArgs", Arrays.asList(new String[] {"arg1", "arg2"}), "default")); assertEquals("default", rc.getMessage("testa", "default")); assertEquals("default", rc.getMessage("testa", (List) null, "default")); MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] {"test"}); assertEquals("test message", rc.getMessage(resolvable)); }
@Test @SuppressWarnings("rawtypes") public void requestContext() throws ServletException { PageContext pc = createPageContext(); RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest()); assertEquals("theme test message", rc.getThemeMessage("themetest")); assertEquals("theme test message", rc.getThemeMessage("themetest", (String[]) null)); assertEquals("theme test message", rc.getThemeMessage("themetest", "default")); assertEquals("theme test message", rc.getThemeMessage("themetest", (Object[]) null, "default")); assertEquals("theme test message arg1", rc.getThemeMessage("themetestArgs", new String[] {"arg1"})); assertEquals("theme test message arg1", rc.getThemeMessage("themetestArgs", Arrays.asList(new String[] {"arg1"}))); assertEquals("default", rc.getThemeMessage("themetesta", "default")); assertEquals("default", rc.getThemeMessage("themetesta", (List) null, "default")); MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] {"themetest"}); assertEquals("theme test message", rc.getThemeMessage(resolvable)); }