网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBootThymeleaf实现国际化的方法详解

前言

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的蓬莱网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • JSP

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

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

添加Thymeleaf依赖

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


 org.springframework.boot
 spring-boot-starter-thymeleaf

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




 
 Insert title here

 





Fluid jumbotron

This is a modified jumbotron that occupies the entire horizontal space of its parent.

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

 4.0.0
 
  org.springframework.boot
  spring-boot-starter-parent
  2.1.7.RELEASE
   
 
 com.easy
 template-thymeleaf
 0.0.1
 template-thymeleaf
 Demo project for Spring Boot

 
  1.8
  UTF-8
  UTF-8
  UTF-8
 

 
  
  
   com.h3database
   h3
   runtime
  

  
   org.springframework.boot
   spring-boot-starter-thymeleaf
  

  
   org.springframework.boot
   spring-boot-starter-web
  

  
   org.springframework.boot
   spring-boot-starter-test
   test
  

  
   org.projectlombok
   lombok
   true
  

 

 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 

运行示例

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

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

2.运行效果分别如下

默认为中文语言环境

Spring Boot Thymeleaf实现国际化的方法详解

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

Spring Boot Thymeleaf实现国际化的方法详解

资料

  • Spring Cloud Feign 示例源码
  • Spring Boot、Spring Cloud示例学习

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对创新互联的支持。


新闻标题:SpringBootThymeleaf实现国际化的方法详解
本文网址:http://bjjierui.cn/article/gjjgge.html

其他资讯