Results 1 to 3 of 3

Thread: Spring MVC and ICU ULocale

  1. #1
    Join Date
    Feb 2011
    Posts
    22

    Question Spring MVC and ICU ULocale

    I am working on adding a Spring MVC module to an existing application. This application uses the ICU library for its internationalization, in particular the ULocale object. I am aware that the Spring MVC library automatically links each request with a java.util.Locale object so that it can be included in a Controller method as an argument. I would like to do the same thing with the ICU ULocale object. Does anyone have any advice on how best to accomplish this?

    Here is an example of what I would like to be able to do:
    Code:
    @RequestMapping("/helloWorld")
    public ModelAndView exampleControllerMethod(@RequestParam("id") param, ULocale icuLocale) {
        //...
    }
    The simplest solution I can think of is to write a simple converter that takes the java.util.Locale object that the Spring MVC LocaleResolver creates and pass it into an ICU ULocale object (The ULocale constructor takes a java.util.Locale). If I do this, does anyone know what converter I should use/interface I should implement?

    Another possibility is to figure out how to make a similar LocaleResolver structure for the ULocale object. Advice on how to do that would be greatly appreciated. Thanks for your help.
    Last edited by ttmrb; Mar 11th, 2012 at 10:45 PM.

  2. #2
    Join Date
    Feb 2011
    Posts
    22

    Default

    Any help would be greatly appreciated. Thanks.

  3. #3
    Join Date
    Feb 2011
    Posts
    22

    Default

    I finally managed to figure out how to accomplish my goal above. I am posting the answer for those who may stumble upon this in the future.

    In Spring 3.1 there is a new interface called HandlerMethodArgumentResolver. It allows for the translation of web requests into the correct method argument. I created a new implementation of this interface called ULocaleMethodArgumentResolver which translates the java.util.Locale that the standard Spring MVC LocalResolver creates into a ICU ULocale object. Here is the code:
    Code:
    public class ULocaleMethodArgumentResolver implements HandlerMethodArgumentResolver {
        @Override
        public boolean supportsParameter(MethodParameter methodParameter) {
            return methodParameter.getParameterType().equals(ULocale.class);
        }
    
        @Override
        public Object resolveArgument(MethodParameter methodParameter, 
                                      ModelAndViewContainer modelAndViewContainer, 
                                      NativeWebRequest nativeWebRequest, 
                                      WebDataBinderFactory webDataBinderFactory) throws Exception {
            return ULocale.forLocale(nativeWebRequest.getLocale());
        }
    }
    Then in your Spring MVC context file, you add the bean to the argument-resolvers element in the annotation-driven element:
    Code:
    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="com.example.app.ULocaleMethodArgumentResolver"/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
    For those using an older version of Spring, I found the WebArgumentResolver interface. I am not sure how it works but it looks similar.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •