解决方法如下:
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" > <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>
请享用!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)