Skip to content

Instantly share code, notes, and snippets.

@tempredirect
Created July 20, 2012 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tempredirect/3150779 to your computer and use it in GitHub Desktop.
Save tempredirect/3150779 to your computer and use it in GitHub Desktop.
Solution for SO 11577363 - How do I customise the resource key lookup for spring MVC validation messages
package com.yourapplication.web.support;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.DefaultMessageCodesResolver;
import java.util.ArrayList;
import java.util.List;
/**
* This reverses the DefaultMessageCodesResolver convention.
*
* object.field.errorCode
* field.errorCode
* type.errorCode
* errorCode
*/
@Component
public class PostfixMessageCodesResolver extends DefaultMessageCodesResolver {
/**
* Build the code list for the given code and field: an
* object/field-specific code, a field-specific code, a plain error code.
* @return the list of codes
*/
@Override
public String[] resolveMessageCodes(String errorCode, String objectName, String field, Class<?> fieldType) {
List<String> codeList = new ArrayList<String>();
List<String> fieldList = new ArrayList<String>();
buildFieldList(field, fieldList);
for (String fieldInList : fieldList) {
codeList.add(postProcessMessageCode( objectName + CODE_SEPARATOR + fieldInList + CODE_SEPARATOR + errorCode ));
}
int dotIndex = field.lastIndexOf('.');
if (dotIndex != -1) {
buildFieldList(field.substring(dotIndex + 1), fieldList);
}
for (String fieldInList : fieldList) {
codeList.add(postProcessMessageCode(fieldInList + CODE_SEPARATOR + errorCode ));
}
if (fieldType != null) {
codeList.add(postProcessMessageCode(fieldType.getName() + CODE_SEPARATOR + errorCode));
}
codeList.add(postProcessMessageCode(errorCode));
return StringUtils.toStringArray(codeList);
}
@Override
public String[] resolveMessageCodes(String errorCode, String objectName) {
return new String[] {
postProcessMessageCode( objectName + CODE_SEPARATOR + errorCode ),
postProcessMessageCode(errorCode)};
}
}
package com.persephone.web;
import com.yourapplication.web.support.PostfixMessageCodesResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import javax.annotation.PostConstruct;
/**
*
*/
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
// this isn't complete...only shows the hook for the messageCodes resolver
@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
@PostConstruct
public void init() {
ConfigurableWebBindingInitializer bindingInitializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter.getWebBindingInitializer();
bindingInitializer.setMessageCodesResolver(new PostfixMessageCodesResolver());
}
}
@tempredirect
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment