SpringMVC + Freemarker 国际化
来源:互联网 发布:淘宝卖家如何发货 编辑:程序博客网 时间:2024/05/20 22:26
1.xml配置文件
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="configLocation" value="classpath:conf/freemarker.properties" />
<property name="templateLoaderPath" value="/pages/" />
<property name="defaultEncoding" value="UTF8" />
<property name="freemarkerVariables">
<map>
<!-- <entry key="picHostUrl" value="${upload.server.url}" />
<entry key="defaultImage" value="${default.image}" />-->
<entry key="buildNumber" value="${deployDate}" />
<entry key="auto_import" value="layout/spring.ftl as spring" />
</map>
</property>
</bean>
<mvc:resources mapping="/statics/**" location="/statics/" cache-period="31536000"/>
<!-- 拦截器配置,拦截顺序:先执行后定义的,排在第一位的最后执行。-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- 配置国际化资源文件路径 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<!-- 定义消息资源文件的相对路径 -->
<value>messages/messages</value>
</property>
</bean>
2.messages/messages目录下建立属性文件
hi=hellosomething=The People's Republic of China
Chinese=Chinese
English=English
index=Index
welcome=Welcome
OtherPage=Other Page
hi=\u4F60\u597D
something=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD
Chinese=\u4E2D\u6587
English=\u82F1\u6587
OtherPage=\u5176\u4ED6\u9875\u9762
index=\u9996\u9875
welcome=\u6B22\u8FCE
3.切换的类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@Controller
@RequestMapping(value = "/global")
public class GlobalController {
@RequestMapping(value="/changeLocale", method = {RequestMethod.GET})
public String test(HttpServletRequest request,Model model, @RequestParam(value="langType", defaultValue="zh") String langType){
if(langType.equals("zh")){
Locale locale = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else if(langType.equals("en")){
Locale locale = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
return "redirect:"+request.getParameter("url");
}
}
java类中引入如下:@Autowired
protected MessageSource messageSource;
4.
- SpringMVC + FreeMarker 国际化使用方法
- SpringMVC + Freemarker 国际化
- SpringMVC+FreeMarker+MyBatis框架搭建之国际化
- springmvc国际化+freemarker整合 一步步走来
- springmvc,freemarker,spring.ftl实现国际化配置解决方案
- SpringMVC+Freemarker
- SpringMVC国际化
- SpringMVC国际化
- SpringMVC 国际化
- SpringMVC国际化
- SpringMVC 国际化
- SpringMVC-国际化
- springmvc 国际化
- SpringMVC国际化
- springmvc国际化
- 【SpringMVC】【国际化】
- SpringMVC国际化
- SpringMVC国际化
- android studio打包生成aar文件并在其他工程引用aar包
- 89C52单片机之串口
- Jenkins邮件通知之Notification插件
- Webstorm快捷键速记(Mac版)
- AS项目运行正常,但查看代码飘红the file size exceeds configured limit.
- SpringMVC + Freemarker 国际化
- Gson基本用法
- 无向图.md
- [leetcode]: 606. Construct String from Binary Tree
- 排名前三的python 开源 IDE
- 分类的树状处理
- jQuery转Vue项目实践总结
- 奇异值分解(Sigular Value Decomposition)
- jstl select <c:if test下拉菜单不能被选中!