Spring Boot Thymeleaf實(shí)現(xiàn)國(guó)際化的方法詳解
前言
開(kāi)發(fā)傳統(tǒng)Java WEB工程時(shí),我們可以使用JSP頁(yè)面模板語(yǔ)言,但是在SpringBoot中已經(jīng)不推薦使用了。SpringBoot支持如下頁(yè)面模板語(yǔ)言
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- JSP
上面并沒(méi)有列舉所有SpringBoot支持的頁(yè)面模板技術(shù)。其中Thymeleaf是SpringBoot官方所推薦使用的,下面來(lái)談?wù)凾hymeleaf實(shí)現(xiàn)應(yīng)用國(guó)際化方法。
ps:當(dāng)然現(xiàn)在開(kāi)發(fā)基本上是前后端分離了,但是難免需要維護(hù)遺留項(xiàng)目或沒(méi)有條件前后端分離的團(tuán)隊(duì)還是有很多的,這時(shí)候?qū)W會(huì)必要的前端技能,能達(dá)到事半功倍的效果。
添加Thymeleaf依賴(lài)
要想使用Thhymeleaf,首先要在pom.xml文件中單獨(dú)添加Thymeleaf依賴(lài)。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Spring Boot默認(rèn)存放模板頁(yè)面的路徑在src/main/resources/templates或者src/main/view/templates,這個(gè)無(wú)論是使用什么模板語(yǔ)言都一樣,當(dāng)然默認(rèn)路徑是可以自定義的,不過(guò)一般不推薦這樣做。另外Thymeleaf默認(rèn)的頁(yè)面文件后綴是.html
什么是國(guó)際化
國(guó)際化(internationalization)是設(shè)計(jì)和制造容易適應(yīng)不同區(qū)域要求的產(chǎn)品的一種方式。它要求從產(chǎn)品中抽離所有地域語(yǔ)言,國(guó)家/地區(qū)和文化相關(guān)的元素。換言之,應(yīng)用程序的功能和代碼設(shè)計(jì)考慮在不同地區(qū)運(yùn)行的需要,其代碼簡(jiǎn)化了不同本地版本的生產(chǎn)。開(kāi)發(fā)這樣的程序的過(guò)程,就稱(chēng)為國(guó)際化。
Spring Boot Thymeleaf 代碼實(shí)現(xiàn)國(guó)際化
1.配置文件代碼WebConfiguration.java
package com.easy.templateThymeleaf.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("es", "ES"));
return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
2.控制器代碼IndexController.java、LocaleController.java
package com.easy.templateThymeleaf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
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 java.util.Locale;
@Controller
public class IndexController {
@Autowired
private MessageSource messageSource;
@RequestMapping(value = {"/index", "/"}, method = RequestMethod.GET)
public String index(Model model, Locale locale) {
model.addAttribute("title", messageSource.getMessage("text.title", null, locale));
return "index";
}
}
package com.easy.templateThymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class LocaleController {
@GetMapping(value = "/locale")
public String localeHandler(HttpServletRequest request) {
String lastUrl = request.getHeader("referer");
return "redirect:" + lastUrl;
}
}
3.靜態(tài)頁(yè)面代碼index.html
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}">Insert title here</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-danger">
<a class="navbar-brand" th:href="@{'/'}">I18N Demo</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" th:href="@{'/'}" th:text="#{text.home}">Home</a>
</li>
</ul>
<ul class="navbar-nav navbar-right">
<li class="dropdown">
<button th:text="#{text.language}" class="btn btn-danger dropdown-toggle" type="button"
id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
</button>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" th:href="@{/locale(lang=es_ES)}"
th:text="#{text.language.chinese}">中文</a>
<a class="dropdown-item" th:href="@{/locale(lang=en_US)}"
th:text="#{text.language.english}">英語(yǔ)</a>
</div>
</li>
</ul>
</div>
</nav>
<div class="container" style="margin-top:50px">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4" th:text="#{text.home.message}">Fluid jumbotron</h1>
<p class="lead" th:text="#{text.description}">This is a modified jumbotron that occupies the entire
horizontal space of its parent.</p>
</div>
</div>
</div>
<footer>
<script th:src="@{/js/jquery-3.3.1.min.js}"></script>
<script th:src="@{/js/popper.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
</footer>
</body>
</html>
4.語(yǔ)言配置文件
中文簡(jiǎn)體語(yǔ)言配置文件messages.properties
text.title=國(guó)際化示例 text.home=首頁(yè) text.language=語(yǔ)言 text.language.chinese=中文(簡(jiǎn)體) text.language.english=英語(yǔ) text.home.message=你好,歡迎你 text.description=這是個(gè)國(guó)際化模板示例
英文語(yǔ)言配置文件messages.properties
text.title=Application title text.home=Home text.language=Language text.language.chinese=Chinese text.language.english=English text.home.message=Hi, welcome! text.description=It is a i18n demo
5.最后貼上maven配置文件pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.easy</groupId>
<artifactId>template-thymeleaf</artifactId>
<version>0.0.1</version>
<name>template-thymeleaf</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<encoding>UTF-8</encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
運(yùn)行示例
1.找到TemplateThymeleafApplication.java文件運(yùn)行示例
地址欄輸入: http://localhost:8080/
2.運(yùn)行效果分別如下
默認(rèn)為中文語(yǔ)言環(huán)境

切換到英文環(huán)境后,界面效果如下

資料
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
- springboot如何使用thymeleaf完成頁(yè)面緩存
- 解決springboot+shiro+thymeleaf頁(yè)面級(jí)元素的權(quán)限控制問(wèn)題
- 解決Springboot項(xiàng)目打包后的頁(yè)面丟失問(wèn)題(thymeleaf報(bào)錯(cuò))
- SpringBoot 利用thymeleaf自定義錯(cuò)誤頁(yè)面
- springboot如何使用thymeleaf模板訪(fǎng)問(wèn)html頁(yè)面
- spring boot使用thymeleaf跳轉(zhuǎn)頁(yè)面實(shí)例代碼
- springboot+thymeleaf國(guó)際化之LocaleResolver接口的示例
- Spring?boot?Thymeleaf配置國(guó)際化頁(yè)面詳解
相關(guān)文章
Java 獲取服務(wù)器環(huán)境的實(shí)例詳解
這篇文章主要介紹了Java 獲取服務(wù)器環(huán)境的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例和輸出結(jié)果,希望能幫助大家理解,需要的朋友可以參考下2017-07-07
MybatisPlus 不修改全局策略和字段注解如何將字段更新為null
這篇文章主要介紹了MybatisPlus 不修改全局策略和字段注解如何將字段更新為null,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Opencv實(shí)現(xiàn)身份證OCR識(shí)別的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Opencv實(shí)現(xiàn)身份證OCR識(shí)別功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2024-03-03
Java java.lang.InstantiationException異常案例詳解
這篇文章主要介紹了Java java.lang.InstantiationException異常案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
java設(shè)計(jì)模式之工廠(chǎng)模式實(shí)例詳解
這篇文章主要介紹了java設(shè)計(jì)模式之工廠(chǎng)模式,結(jié)合具有實(shí)例形式分析了java工廠(chǎng)模式的概念、原理、實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2017-09-09
使用jxls自定義命令設(shè)置動(dòng)態(tài)行高
這篇文章主要介紹了使用jxls自定義命令設(shè)置動(dòng)態(tài)行高,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
java實(shí)現(xiàn)百度坐標(biāo)的摩卡托坐標(biāo)與火星坐標(biāo)轉(zhuǎn)換的示例
這篇文章主要介紹了java實(shí)現(xiàn)百度坐標(biāo)的摩卡托坐標(biāo)與火星坐標(biāo)轉(zhuǎn)換的示例,需要的朋友可以參考下2014-03-03
springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析
這篇文章主要介紹了springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

