SpringMVC国际化,通过登录页⾯选择语⾔,系统根据配置信
息显⽰什么语⾔
在页⾯通过spring标签的那种效果其他帖⼦都已经有了,这个我主要说的是,将提⽰信息添加⼊资源配置⽂件中,根据⽤户登录的时候去选择的语⾔,来动态的去显⽰什么语⾔。
1、⾸先得有⼀个spring mvc 项⽬。
2、    在l⽂件中加⼊如下配置
<bean id="messageSource" class="com.hxd.isp.publics.utils.IspResourceBundleMessageSource">
<!-- 国际化资源⽂件配置,指定properties⽂件存放位置 -->
<!-- <property name="basename" value="classpath:message/**" /> -->
<property name="basenames">
<list>
<value>classpath:message/**/*.properties</value>
</list>
</property>
<!-- 如果在国际化资源⽂件中不到对应代码的信息,就⽤这个代码作为名称  -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 动态切换国际化 ,国际化放在session中 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<mvc:interceptors>
<!-- 国际化操作 如果采⽤基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 通过这个参数来决定获取那个配置⽂件 -->
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>
其中的IspResourceBundleMessageSource 需要根据路径获取出来的资源配置⽂件重新给basenames set⼀下值,因为我们的value中的值 只需要配置⽂件的“_”下划线之前的⽂件名称,⾄于后⾯的国家简称、语⾔简称都不需要。
3、给basenames数组设置值,
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import XLogger;
import XLoggerFactory;
import t.support.ReloadableResourceBundleMessageSource;
import io.Resource;
import io.support.PathMatchingResourcePatternResolver;
public class IspResourceBundleMessageSource extends ReloadableResourceBundleMessageSource{
private static String PROPERTY_POSTFIX = "_";
private static String PROPERTY_POSTFIX_A = ".properties";
private static final XLogger log = XLogger(IspResourceBundleMessageSource.class);
private PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
public void setBasenames(String[] basenames) {
if (basenames == null)
{
super.setBasenames(null);
}
List baseNameList = new ArrayList();
for (String baseName : basenames)
{
try
{
Resource[] resources = Resources(baseName);
for (Resource resource : resources)
{
String fileName = URI().toString();
int size = fileName.indexOf(PROPERTY_POSTFIX);
if(size==-1){
size = fileName.indexOf(PROPERTY_POSTFIX_A);
}
baseNameList.add(fileName.substring(0, size));
if (!this.logger.isDebugEnabled())
continue;
this.logger.debug("Add properties file: [" +
}
}
catch (IOException e)
{
log.throwing(e);
}
super.setBasenames((String[])Array(
new String[baseNameList
.size()]));
}
}
}springmvc常用标签
在⽅法中写了两个 PROPERTY_POSTFIX的⽬的是:如果你只有某些模块需要进⾏语⾔之间的切换,就造成有的提⽰信息的配置⽂件名称格式不是有“_”下划线格式的,加两个的效果就是既可以读取需要语⾔切换的配置⽂件也可以读取不需要语⾔切换的配置⽂件。
4、登录页⾯添加选择语⾔框。
<tr>
<td class="listt">系统语⾔:</td>
<td colspan="2" align="left">
<div >
<input type="radio" id="language" value="zh_CN" name="language" checked="true"><span
class="blackgray" >中⽂</span>
</div>
<div >
<input type="radio" id="language" value="en_US" name="language"><span class="blackgray"
>ENGLISH</span>
</div>
</td>
</tr>
5、新建⼀个类继承UsernamePasswordAuthenticationFilter 重写setDetails⽅法
@Override
protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
Map<String, Object> detailMap = new HashMap<String, Object>(3);
detailMap.put("language", Parameter("language"));
detailMap.put("password", (String) Credentials());
detailMap.put(SecurityConstant.REQUEST_DETAIL_KEY, authenticationDetailsSource.buildDetails(request));
authRequest.setDetails(detailMap);
}
6、新建⼀个类继承DaoAuthenticationProvider 重写additionalAuthenticationChecks(UserDetails userDetails,
UsernamePasswordAuthenticationToken authentication)⽅法,下⾯为和获取语⾔相关的代码。
UserDetail person = (UserDetail) userDetails;
Map<String, Object> detailMap =(Map) Details();
if(!StringUtils.isEmpty(detailMap)&&!StringUtils.("language"))){
person.("language").toString());
}
以上操作完成后,就可以通过
Authentication authentication = null;
SecurityContext context = Context();
if (context != null) {
authentication  =Authentication();
}
if (authentication != null) {
Object principal = Principal();
if (principal instanceof User) {
//这⾥就可以获取到language这个参数
}
}
在⽤MessageSource的getMessage 之前, Locale locale = new Locale(language); 直接把这个locale传⼊getMessage⽅法中,就可以⾃动去获取不同⼈配置资源⽂件了

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。