Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用messageSource解析spring消息代码_Java_Spring_Localization_Resourcebundle - Fatal编程技术网

Java 无法使用messageSource解析spring消息代码

Java 无法使用messageSource解析spring消息代码,java,spring,localization,resourcebundle,Java,Spring,Localization,Resourcebundle,我正在尝试在spring中连接一个messageSource以用于我的应用程序。它不工作,导致以下错误: org.springframework.context.NoSuchMessageException:在区域设置“en”的代码“validation\u required”下未找到任何消息 myapplicationContext.xml包含此messageSource定义: <bean id="messageSource" class="org.springfr

我正在尝试在spring中连接一个messageSource以用于我的应用程序。它不工作,导致以下错误:

org.springframework.context.NoSuchMessageException:在区域设置“en”的代码“validation\u required”下未找到任何消息

myapplicationContext.xml包含此messageSource定义:

   <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:messages</value>
            </list>
        </property>
    </bean>
最后,生成错误的调用是:

String message = messageSource.getMessage("validation_required", null, Locale.ENGLISH);

现在有人能帮我吗?

问题在于您定义资源包的方式和指定的区域设置(它们与资源包的不匹配。请将您的包重命名为“messages\u en.properties”或使用新的区域设置(“en”、“US”)调用“getMessage(…)”.我更喜欢第一种选择。

你的路径似乎不正确。
由于您的bundle位于/WEB-INF/classes/messages/messages\u en\u US.properties下,因此您的basename设置应该如下所示:classpath:messages/messages(在本例中,basename表示路径和属性文件前缀)。

我使用以下bean,它在不指定文件路径的情况下工作正常:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" abstract="false"
      scope="singleton" lazy-init="default">
    <property name="basename" value="messages"/>
</bean>
是否出现异常?请尝试使用此文件名messages\u en.properties或messages\u us\u en.properties 查看注释以获取字符串

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();
    }


}

这是非常低效的,每次您需要一个字符串(带有@Resource部分)链接时,都会实例化整个MSG对象
    messageSource.getMessage("validation_required", null, null);
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();
    }


}