Spring整合FreeMarker本地化动态设置

2024-09-07 17:32

本文主要是介绍Spring整合FreeMarker本地化动态设置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring整合FreeMarker进行国际化的过程很简单,将spring-webmvc里的org/springframework/web/servlet/view/freemarker下的spring.ftl include到各个要国际化的ftl文件里就行了,比如将这个spring.ftl复制在ftl文件目录的common下,可以这样直接在ftl里include进来

<#import "/common/spring.ftl" as spring/>  

当然也可以在spring配置文件里对freemarkerConfig配置的时候自动导入

复制代码
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
...  <property name="freemarkerSettings">  <props>  <prop key="defaultEncoding">UTF-8</prop>  ...  <prop key="auto_import">common/spring.ftl as spring</prop>  </props>  </property>  
复制代码

 

然后在spring的配置文件里声明下

复制代码
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  <property name="basenames">  <list>  <value>MessageResources</value>  </list>  </property>  
</bean>  
复制代码

 

在源文件目录resource下有相应的MessageResources.zh_CN.properties和MessageResources.en_US.propertes等属性文件

 

在ftl文件里这样使用

<@spring.message "hello"/>  

 

这样系统会根据用户当前环境下的locale进行国际化。但是如果要自己手动指定某个用户的locale要怎么办呢?

 

参考了下org.springframework.web.servlet.i18n.LocaleChangeInterceptor对locale的设置,首先要在spring的配置文件里声明一个LocaleResolver (比如 CookieLocaleResolver,不然的话后面的操作会出现异常),我这里对Locale的设置只要在session范围里就行了

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">  <property name="defaultLocale" value="zh" />  
</bean>

 

接着在spring的controller里控制locale的方法里加入以下代码就可以了

复制代码
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);  
if (localeResolver == null) {  throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");  
}  
LocaleEditor localeEditor = new LocaleEditor();  
localeEditor.setAsText(lang);  
localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());  
复制代码

 

最后总结下我的方式

 

1.spring的配置里必须要有的

复制代码
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>  <property name="freemarkerSettings">  <props>  <prop key="defaultEncoding">UTF-8</prop>  <!-- 在生产环境下更新模板的间隔要根据实际情况设置,这里为开发方便设置为5秒 -->  <prop key="template_update_delay">5</prop>  <!-- 我把spring.ftl里的内容和项目里要用到的公共的macro合在一起了 -->  <prop key="auto_import">common/website.ftl as website</prop>  </props>  </property>  </bean>  <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  <property name="cache" value="true"/>  <property name="prefix" value=""/>  <property name="suffix" value=".ftl"/>      <property name="contentType" value="text/html;charset=utf-8"></property>  </bean>  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">  <property name="defaultLocale" value="zh" />  
</bean>   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  <property name="basenames">  <list>  <value>MessageResources</value>  </list>  </property>  
</bean>  
复制代码

 

 2.在设置用户Locale的Controller里设置Locale

复制代码
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);  
if (localeResolver == null) {  throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");  
}  
LocaleEditor localeEditor = new LocaleEditor();  
localeEditor.setAsText(lang);  
localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());    
复制代码

 

3.在ftl里这样使用

<@website.message "hello"/>  

 

来源:http://yvonxiao.iteye.com/blog/1005183

这篇关于Spring整合FreeMarker本地化动态设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145692

相关文章

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

一文详解Java Condition的await和signal等待通知机制

《一文详解JavaCondition的await和signal等待通知机制》这篇文章主要为大家详细介绍了JavaCondition的await和signal等待通知机制的相关知识,文中的示例代码讲... 目录1. Condition的核心方法2. 使用场景与优势3. 使用流程与规范基本模板生产者-消费者示例

Sentinel 断路器在Spring Cloud使用详解

《Sentinel断路器在SpringCloud使用详解》Sentinel是阿里巴巴开源的一款微服务流量控制组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、... 目录Sentinel 介绍同类对比Hystrix:Sentinel:微服务雪崩问题问题原因问题解决方案请

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的