科技资讯眺望|Spring Boot 12 国际化( 二 )
12.3 控制器和视图
I18nController.java(在参数上设置本地化)
package me.laocat.i18n;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class I18nController {@RequestMapping(value = http://kandian.youth.cn/index/{ “/”, “/login1” })public String staticResource(Model model) {return “login1”;}}
login1.html (Thymeleaf 视图)
Login (En)|Login (Zh)
12.4 URL上的语言环境信息如果您要构建一个多语言网站 , 其语言环境信息位于URL上 。 您需要更改一些配置:
- http://localhost:8080/SomeContextPath/en/login2
- http://localhost:8080/SomeContextPath/zh/login2
UrlLocaleInterceptor.java
package me.laocat.i18n.interceptors;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.LocaleResolver;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import org.springframework.web.servlet.support.RequestContextUtils;public class UrlLocaleInterceptor extends HandlerInterceptorAdapter {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);if (localeResolver == null) {throw new IllegalStateException(“找不到LocaleResolver:不在DispatcherServlet请求中??”);}// 从LocaleResolver获取区域设置Locale locale = localeResolver.resolveLocale(request);localeResolver.setLocale(request, response, locale);return true;}}
UrlLocaleResolver.javapackage me.laocat.i18n.resolver;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.LocaleResolver;@Configurationpublic class UrlLocaleResolver implements LocaleResolver {private static final String URL_LOCALE_ATTRIBUTE_NAME = “URL_LOCALE_ATTRIBUTE_NAME”;@Bean(name = “messageSource”)@Overridepublic Locale resolveLocale(HttpServletRequest request) {// ==> /SomeContextPath/en/…// ==> /SomeContextPath/zh/…// ==> /SomeContextPath/WEB-INF/pages/…String uri = request.getRequestURI();System.out.println(“URI=” + uri);String prefixEn = request.getServletContext().getContextPath() + “/en/”;String prefixZh = request.getServletContext().getContextPath() + “/zh/”;Locale locale = null;// Englishif (uri.startsWith(prefixEn)) {locale = Locale.ENGLISH;}// Chinaelse if (uri.startsWith(prefixZh)) {locale = new Locale(“zh”, “CN”);}if (locale != null) {request.getSession().setAttribute(URL_LOCALE_ATTRIBUTE_NAME, locale);}if (locale == null) {locale = (Locale) request.getSession().getAttribute(URL_LOCALE_ATTRIBUTE_NAME);if (locale == null) {locale = Locale.ENGLISH;}}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {// Nothing}}
- 所持股份|万兴科技:公司控股股东、实际控制人吴太兵质押150万股
- 发布公告|数量过半!博创科技:天通股份累计减持约150万股
- 英雄科技聊数码|蔡崇信有实力买下篮网,那身价3200亿的马云,能买下几支NBA球队
- 科技前沿阵地|涨疯了!海思安防芯片遭哄抬“围剿”
- 月影浓|吴亦凡机械造型走秀 垫肩披风搭银框眼镜科技感足
- 资讯早知道|9年后再看《步步惊心》,发现若曦无论跟谁都不会幸福
- 中国历史发展过程|中国历史发展过程.中国的科技史界过去半个多世纪
- 天津|桂发祥:不再持有昆汀科技股份
- 消费|减持!天通股份:减持博创科技约32万股
- 处罚|老周侃股:吉鑫科技大股东应补偿踩雷投资者