SpringBoot學習篇5[國際化、統一異常處理]

1 Spring Boot對國際化的支持

1.1 語言包創建

  1. 右鍵resources->new->directory,新創建i18n(國際化的縮寫)文件夾。

  2. 右鍵新創建的文件夾i18n->new->Resource Bundle
    在這裏插入圖片描述

  3. 點擊+按鈕
    在這裏插入圖片描述

  4. 添加zh_CN、en_US兩項,並起名字爲message(如果名字任意取
    在這裏插入圖片描述

  5. 配置spring.messages.basename屬性

    spring.messages.basename=i18n.message
    
  6. 添加key
    在這裏插入圖片描述

  7. 以要添加login界面的多語言爲例
    在這裏插入圖片描述
    右邊三欄分別是默認顯示的語言、英文、中文,這樣添加的好處是同時添加了多個語言包。
    在這裏插入圖片描述

1.2 根據本地語言自動切換語言包

  1. 修改登陸界面
    注:login.html一定要放到模板目錄下,因爲我們將用Thymeleaf的message表達式獲取配置文件裏的內容

    <!DOCTYPE html>
    <html lang="en">
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title th:text="#{login.title}"></title>
    </head>
    <body>
        <form action="/login" method="POST">
            [[#{login.username}]]<input type="text" name="userName"><br>
            [[#{login.password}]]<input type="password" name="password"><br>
            <input type="submit" th:value="#{login.login}">
            <input type="reset" th:value="#{login.reset}">
        </form>
    </body>
    </html>
    
  2. 效果
    在這裏插入圖片描述

  3. 設置一下瀏覽器語言
    在這裏插入圖片描述

  4. 刷新一下,可以看到我們的頁面也改變了語言
    在這裏插入圖片描述

1.3 實現手動切換

根據本地語言自動切換語言實現了,手動切換我們也實現一下。
在index.html頁面添加以下代碼

<br><a th:href="@{/(local=zh_CN)}">中文</a>
<a th:href="@{/(local=en_US)}">English</a>

接下來要做的就是自定義區域解析器
實現步驟:

  1. 自定義類MyLocaleResolver,實現LocaleResolver接口
  2. 重寫resolveLocale方法
  3. 替換默認區域解析器
    自定義MyLocaleResolver類代碼如下:
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //獲取自定義請求頭信息
        String local = httpServletRequest.getParameter("local");
        System.out.println(local);
        //獲取系統默認
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(local))
        {
            String[] split = local.split("_");
            //第一個參數爲語言代碼,第二個參數爲國家代碼
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

在配置類中注入Spring IOC容器:
注 :返回值和方法名要和下面保持一致,否則無法替換原有的區域解析器

	@Bean
    public LocaleResolver localeResolver()
    {
        return new MyLocaleResolver();
    }

效果
在這裏插入圖片描述
在這裏插入圖片描述

2 統一異常處理

先看一下在正常情況訪問一個不存在的頁面會顯示什麼:
在這裏插入圖片描述
當服務器發生內部錯誤時:
故意製造的錯誤

	@GetMapping("/user/error")
    public String error()
    {
        int i = 1/0;

        return "success";
    }

在這裏插入圖片描述
無論是4xx錯誤,還是5xx錯誤,直接採用Spring Boot默認的方式展現給用戶,看起來並不友好。
Spring Boot針對錯誤頁面,給出了特殊處理,可以用靜態資源頁面展示錯誤頁面,也可以用模板引擎頁面展示錯誤頁面。

2.1 靜態資源頁面展示錯誤頁面

在static/error或public/error目錄下存放4xx.html、5xx.html(如果有更加詳細的名字,如404.html、500.html,則優先用詳細錯誤頁面
在這裏插入圖片描述
發生500錯誤時:
在這裏插入圖片描述
發生404錯誤時:
在這裏插入圖片描述

2.2 模板引擎頁面展示錯誤頁面,

在templates/error目錄下存放,最後展示的效果和上面一樣,就不展示了。

2.3 優先級

以發生404錯誤爲例,採用精確優先原則

  1. 當有更加詳細的錯誤頁面時,用更加詳細的錯誤頁面
    templates/error/404.html > static/error/404.html > templates/error/4xx.html > static/error/4xx.html
  2. 靜態頁面和模板頁面存在相同名字的錯誤頁面時,優先用模板頁面下的

2.4 錯誤頁面可以獲取到的內容

timestamp:時間戳
status:狀態碼
error:錯誤提示
exception:異常對象
message:異常消息
errors:JSR303數據校驗出現的錯誤

<body>
    錯誤404。抱歉,該頁面不存在。。。。
    [[${timestamp}]]
</body>

在這裏插入圖片描述

2.5 添加自定義錯誤信息

除了3.6.4列出的錯誤信息外,Spring Boot還支持添加自定義錯誤信息。
步驟:

  1. 編寫類,繼承DefaultErrorAttributes類
  2. 重寫getErrorAttributes,並在裏面添加自定義信息
  3. 將其添加至Spring IOC容器
	@Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String,Object> map = super.getErrorAttributes(webRequest,includeStackTrace);
        map.put("email","[email protected]");
        return map;
    }

運行效果:
在這裏插入圖片描述

發佈了41 篇原創文章 · 獲贊 18 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章