开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。SpringBoot支持如下页面模板语言

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • JSP

上面并没有列举所有SpringBoot支持的页面模板技术。其中Thymeleaf是SpringBoot官方所推荐使用的,下面来谈谈Thymeleaf实现应用国际化方法。

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

添加Thymeleaf依赖

要想使用Thhymeleaf,首先要在pom.xml文件中单独添加Thymeleaf依赖。

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

Spring Boot默认存放模板页面的路径在src/main/resources/templates或者src/main/view/templates,这个无论是使用什么模板语言都一样,当然默认路径是可以自定义的,不过一般不推荐这样做。另外Thymeleaf默认的页面文件后缀是.html

什么是国际化

国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素。换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。开发这样的程序的过程,就称为国际化。

Spring Boot Thymeleaf 代码实现国际化

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.静态页面代码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}">英语</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.语言配置文件

中文简体语言配置文件messages.properties

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

英文语言配置文件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>

运行示例

1.找到TemplateThymeleafApplication.java文件运行示例

地址栏输入: http://localhost:8080/

2.运行效果分别如下

默认为中文语言环境

切换到英文环境后,界面效果如下

资料

Spring Boot Thymeleaf 实现国际化的更多相关文章

  1. spring boot + thymeleaf 3 国际化

    在给spring boot 1.5.6 + thymeleaf 3进行国际化时,踩了一个坑(其实不止一个). 现象: 看到了吧, 就是取值的key, 后面被加了_en_US 或 _zh_CN, 以及前 ...

  2. spring boot + Thymeleaf开发web项目

    "Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等.大多数情况下Web应用程序将使用 spring- ...

  3. Spring boot+Thymeleaf+easyui集成:js创建组件页面报错

    开发工具:Ideal 使用场景:Demo 前提:       环境:Spring boot +Thymeleaf+easyui 引入thymeleaf模板引擎 <html lang=" ...

  4. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  5. spring boot + thymeleaf 乱码问题

    spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...

  6. 一个简单的示例在spring boot中实现国际化

    最近在网上找了一个有关账单管理的spring boot项目,其中有一部分是涉及显示国际化信息的,即将页面上的中英文进行转换.因为在这之前这部分内容没有接触过,所以在这记录下过程. 中文效果图如下所示: ...

  7. Spring Boot Thymeleaf 使用详解

    在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...

  8. 细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!

    @ 目录 1. Thymeleaf 简介 2. 整合 Spring Boot 2.1 基本用法 2.2 手动渲染 3. Thymeleaf 细节 3.1 标准表达式语法 3.1.1 简单表达式 3.1 ...

  9. spring boot thymeleaf 标签未关闭报错

    每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code spring boot,input标签未关闭报bug,代码稍有不慎就出小问题,后来百度,goo ...

随机推荐

  1. Vue函数式组件的应用

    一.函数式组件和普通组件的区别 渲染快 没有实例,意味着没有(this) 没有生命周期(没有响应式数据) 二.组件函数的使用 1. 以局部组件为例,将组件标记为 functional=ture; 因为 ...

  2. CSU1784

    题意略. 思路:为了更好地求出一段连续数字的异或和,我们可以用前缀异或和来维护,现在我们只需要考虑每一个在数组中的数字向前异或,且在指定范围内, 异或值为全1的个数有多少个.算出每一个位子能做出的贡献 ...

  3. Leetcode之二分法专题-441. 排列硬币(Arranging Coins)

    Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...

  4. Socket(套接字)在服务器端和客户端之间的基本工作原理

    Socket之间的连接过程主要可以概括为以下三步: 服务器建立监听:客户端初始化Socket动态库后创建套接字,然后指定客户端Socket的地址,循环绑定Socket直至成功,然后开始建立监听,此时客 ...

  5. 边缘缓存模式(Cache-Aside Pattern)

    边缘缓存模式(Cache-Aside Pattern),即按需将数据从数据存储加载到缓存中.此模式最大的作用就是提高性能减少不必要的查询. 1 模式 先从缓存查询数据 如果没有命中缓存则从数据存储查询 ...

  6. NLP(二十三)使用LSTM进行语言建模以预测最优词

    N元模型 预测要输入的连续词,比如 如果抽取两个连续的词汇,则称之为二元模型 准备工作 数据集使用 Alice in Wonderland 将初始数据提取N-grams import nltk imp ...

  7. 爬虫——cookie模拟登陆

    cookie适用于抓取需要登录才能访问的页面网站 cookie和session机制 http协议为无连接协议,cookie: 存放在客户端浏览器,session: 存放在Web服务器 人人网登录案例 ...

  8. vue.js纯前端处理如何将后台返回来的csv数据导出成csv文件

    需要实现一个下载csv文件的功能,但后台没有对这个下载文件进行处理,而是将csv数据传给前台而已,需要前台做一下处理. 这是按钮的代码: <a> <el-button size=&q ...

  9. 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板

    链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网  HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升.   ...

  10. CF990B Micro-World 贪心 第十六

    Micro-World time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...