cannot resolve spring message code using messageSource

前端 未结 4 798
终归单人心
终归单人心 2021-01-06 19:11

I am trying to hook up a messageSource in spring to use for my application. Its not working, gives this error:

org.springframework.context.NoSuchMessa

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-06 19:30

    Try this look at the comment for getting the string

    package yours;
    import java.util.Locale;
    
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Configurable;
    import org.springframework.context.MessageSource;
    import org.springframework.context.i18n.LocaleContextHolder;
    
    /**
     *  
     * Permet la recuperation des String du LocaleContextHolder hors des jsp 
     * Les langues sont placées dans main/ressources/messagesSources.properties 
     * 
     * Example : new MSG("recuperation_name_invalid").value()
     * 
     */
    @Configurable
    public class MSG
    {
    
        private String key;
    
        @Resource(name = "messageSource")
        private MessageSource messageSource;
    
        public MSG(String key)
        {
            super();
            this.key = key;
        }
    
        public String value()
        {
            Locale locale = LocaleContextHolder.getLocale();
    
            return messageSource.getMessage(key, new Object[0], locale);
        }
    
        @Override
        public String toString()
        {
            return value();
        }
    
    
    }
    

提交回复
热议问题