Spring Boot Thymeleaf internationalization

When developing the traditional Java WEB project, we can use the JSP page template language, but not in SpringBoot has been recommended for use. SpringBoot page template supports the following language

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • JSP

Above does not enumerate all of page templates SpringBoot technical support. SpringBoot Thymeleaf which is officially recommended, let's talk about Thymeleaf achieve international application method.

ps:当然现在开发基本上是前后端分离了,但是难免需要维护遗留项目或没有条件前后端分离的团队还是有很多的,这时候学会必要的前端技能,能达到事半功倍的效果。

Add Thymeleaf dependence

To use Thhymeleaf, it must first be added separately in pom.xml file Thymeleaf dependent.

<dependency>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

Spring Boot default path to store the template pages in src / main / resources / templates or src / main / view / templates, that no matter what template language are the same, of course, the default path is customizable, but generally not recommended . In addition Thymeleaf default page file extension is .html

What is international

Internationalization (internationalization) is designed and manufactured easily adapted to different locales of the product. This requires extracting all regional language, country / region and culturally relevant elements from the product. In other words, functional design and code applications need to consider running in different regions, whose source code simplifies the different local editions. The process of developing such a program, called internationalization.

Spring Boot Thymeleaf code for internationalization

Configuration file code 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. The controller code 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. static index.html page code

<!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}">英语</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. Language Profiles

Simplified Chinese language profiles messages.properties

text.title=国际化示例
text.home=首页
text.language=语言
text.language.chinese=中文(简体)
text.language.english=英语 text.home.message=你好,欢迎你 text.description=这是个国际化模板示例

English language profile 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. Finally paste maven pom.xml configuration file

<?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> 

Run the sample

1. Locate the file to run the sample TemplateThymeleafApplication.java

Address bar: http: // localhost: 8080 /

2. The operating results are as follows

The default setting for the Chinese language

After the switch to the English environment, interface effects as follows

data

 
 
Category: the Java

Guess you like

Origin www.cnblogs.com/xichji/p/11627095.html