java - Spring MVC ResourceBundleMessageSource XML 配置

标签 java spring jakarta-ee spring-mvc spring-roo

我想模仿 Grails 的方式来解析 i18n 消息。

在 WEB-INF/i18n/我有以下目录:

admin/messages_EN.properties

admin/messages_FR.properties

网站/messages_EN.properties

网站/messages_FR.properties

请忽略此示例中的语言结尾(EN 和 FR)

在我目前的 xml 配置中:

<!-- Register the welcome.properties -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="defaultEncoding" value="utf-8" />
  <property name="basename" value="/WEB-INF/i18n/" />
</bean>

我在这里寻找的是一种告诉 Spring 在 i18n 下查找 .properties 文件但没有明确告诉它每个子目录是什么的方法。那是没有指向/WEB-INF/i18n/admin/basenames列表>/WEB-INF/i18n/website/

我希望 WEB-INF/i18n/目录是动态的,并且可以创建 bundle (目录)而无需重新修改 xml 配置文件。

我不是想用管理和网站子目录解决这个特定的例子

这可能吗?

谢谢!

最佳答案

解决方法如下:

package com.mypackage.core.src;

import java.io.File;
import java.util.ArrayList;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class UnderDirectoryReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource {

    @Autowired
    ServletContext servletContext;

    public void setWorkingDirectory(String directoryPath) {
        File rootDir = new File( servletContext.getRealPath(directoryPath) );
        ArrayList<String> baseNames = new ArrayList<String>();
        iterateScanDirectoryAndAddBaseNames(baseNames, rootDir);
        setBasenames(baseNames.toArray(new String[baseNames.size()]));
    }

    private void iterateScanDirectoryAndAddBaseNames(ArrayList<String> baseNames, File directory) {
        File[] files = directory.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                iterateScanDirectoryAndAddBaseNames(baseNames, file);
            } else {
                if (file.getName().endsWith(".properties")) {
                    String filePath = file.getAbsolutePath().replaceAll("\\\\", "/").replaceAll(".properties$", "");
                    filePath = filePath.substring(filePath.indexOf("/WEB-INF/"), filePath.length());
                    baseNames.add(filePath);
                    System.out.println("Added file to baseNames: " + filePath);
                }
            }
        }
    }

}

XML 配置:

<bean id="messageSource" class="com.mypackage.core.src.UnderDirectoryReloadableResourceBundleMessageSource">
  <property name="defaultEncoding" value="utf-8" />
  <property name="workingDirectory" value="/WEB-INF/webspring/i18n" />
  <property name="cacheSeconds" value="3" />
  <property name="fallbackToSystemLocale" value="false" />
</bean>

尽情享受吧!

关于java - Spring MVC ResourceBundleMessageSource XML 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11117850/

相关文章:

java - 处理相机调用并预览时出现 javaNullPointer 异常

java - 消息级 Jax-WS 服务

java - jsoup 中的什么方法可以返回修改后的 html?

java - 通过 EJB RMI 传递对象 - NullPointerException

spring - 将图像上传到spring mvc中的文件夹

java - Heroku java.lang.UnsupportedClassVersionErrorUnsupported Major.minor 版本 51.0

java - 更改我的java应用程序的图标

java - Spring MVC 中的条件@RequestBody

java - 在 FreeBSD 共享服务器上安装 GlassFish

java - spring boot扫描并注入(inject)外部非spring bean