1. 程式人生 > >從.Net到Java學習第八篇——SpringBoot實現session共享和國際化

從.Net到Java學習第八篇——SpringBoot實現session共享和國際化

區分 cal request 展示 hang 輸入 nds www target

SpringBoot Session共享

修改pom.xml添加依賴

        <!--spring session-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

添加配置類RedisSessionConfig

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)//默認是1800秒過期,這裏測試修改為60秒
public class RedisSessionConfig {
}

添加一個控制器類SessionController來進行測試

@RestController
public class SessionController {
    @RequestMapping("/uid")
    String uid(HttpSession session) {
        UUID uid 
= (UUID) session.getAttribute("uid"); if (uid == null) { uid = UUID.randomUUID(); } session.setAttribute("uid", uid); return session.getId(); } }

先訪問http://localhost:8083/boot/uid

技術分享圖片

然後修改配置文件application.yml

spring:
  profiles:
    active: test

重新運行IDEA,test配置文件配置的端口是8085,所以瀏覽器輸入http://localhost:8085/boot/uid

技術分享圖片

我們看到兩個uid是一樣的。

在這裏我是使用spring boot redis來實現session共享,你還可以配合使用nginx進行負載均衡,同時共享session。

關於nginx可以參考我的另一篇文章:Nginx詳解-服務器集群

spring boot 國際化

在spring boot中實現國際化是很簡單的的一件事情。

(1)在resources目錄下面,我們新建幾個資源文件,messages.properties相當於是默認配置的,當其它配置中找不到記錄的時候,最後會再到這個配置文件中去查找。

messages.properties
messages_en_US.properties
messages_zh_CN.properties

依次在這三個配置文件中添加如下配置值:

msg=我是中國人
msg=I‘m Chinese
msg=我是中國人

添加完之後,會自動將這幾個文件包在一塊

技術分享圖片

需要註意的是這個命名是有講究的,messages.properties部分是固定的,不同語言的話,我們可以在它們中間用_區分。為什麽是固定的命名,因為源碼是硬編碼這樣命名的。

(2)新建一個配置文件LocaleConfig

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter {

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默認語言
        slr.setDefaultLocale(Locale.CHINA);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 參數名
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

(3)在控制器中,我們添加測試用的方法

    //    i18n
    @RequestMapping("/")
    public String i18n() {
        return "i18n";
    }

    @RequestMapping("/changeSessionLanauage")
    public String changeSessionLanauage(HttpServletRequest request, HttpServletResponse response, String lang){
        System.out.println(lang);
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if("zh".equals(lang)){
            localeResolver.setLocale(request, response, new Locale("zh", "CN"));
        }else if("en".equals(lang)){
            localeResolver.setLocale(request, response, new Locale("en", "US"));
        }
        return "redirect:/";
    }

(4)添加視圖來展示,在templates下新建文件i18n.html,通過#可以直接獲取國際化配置文件中的配置項的值。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>$Title$</title>
</head>
<body>
<a href="/changeSessionLanauage?lang=en">English(US)</a>
<a href="/changeSessionLanauage?lang=zh">簡體中文</a>
<br />

<h3 th:text="#{msg}"></h3>
<h4 th:text="${message}"></h4>
</body>
</html>

(5)運行查看效果

技術分享圖片

從.Net到Java學習第八篇——SpringBoot實現session共享和國際化