Spring MVC ResourceBundleMessageSource XML配置 [英] Spring MVC ResourceBundleMessageSource XML configuration

查看:184
本文介绍了Spring MVC ResourceBundleMessageSource XML配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



admin /messages_EN.properties



admin /messages_FR.properties



网站 /messages_EN.properties



网站 /messages_FR.properties



请在我的xml配置中忽略此示例中的语言结尾(EN和FR)



 <! - 注册welcome.properties  - > 
< bean id =messageSourceclass =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =defaultEncodingvalue =utf-8/>
< property name =basenamevalue =/ WEB-INF / i18n //>
< / bean>

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



我希望WEB-INF / i18n /目录是动态的,而捆绑(目录)可以无需重新编写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;

公共类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(文件文件:文件){
if(file.isDirectory()){
iterateScanDirectoryAndAddBaseNames(baseNames,file);
} else {
if(file.getName()。endsWith(。properties)){
String filePath = file.getAbsolutePath()。replaceAll(\\\ \,/)。replaceAll(。property,);
filePath = filePath.substring(filePath.indexOf(/ WEB-INF /),filePath.length());
baseNames.add(filePath);
System.out.println(添加到baseNames的文件:+ filePath);
}
}
}
}

}

XML配置:

 < bean id =messageSourceclass =com.mypackage。 core.src.UnderDirectoryReloadableResourceBundleMessageSource> 
< property name =defaultEncodingvalue =utf-8/>
< property name =workingDirectoryvalue =/ WEB-INF / webspring / i18n/>
< property name =cacheSecondsvalue =3/>
< property name =fallbackToSystemLocalevalue =false/>
< / bean>

享受!


I would like to mimic the Grails way of resolving i18n messages.

In WEB-INF/i18n/ I have the following directories:

admin/messages_EN.properties

admin/messages_FR.properties

website/messages_EN.properties

website/messages_FR.properties

please ignore the language endings ( EN and FR ) in this example

in my xml configuration I currently have:

<!-- 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>

What I am looking for here, is a way to tell Spring to look for .properties files under i18n but without explicitly telling it what each subdirectory is. That is without a list of basenames that points to /WEB-INF/i18n/admin/ and /WEB-INF/i18n/website/

I want the WEB-INF/i18n/ directory to be dynamic, and that bundles ( directories ) can be created without having to remodify the xml configuration file.

I am not trying to solve this particular example with admin and website sub directories

Is this possible?

Thanks!

解决方案

Here is the solution:

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 config:

<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>

Enjoy!

这篇关于Spring MVC ResourceBundleMessageSource XML配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆