当前位置: 代码迷 >> 综合 >> springboot+thymeleaf+i18n 使用例子
  详细解决方案

springboot+thymeleaf+i18n 使用例子

热度:26   发布时间:2024-01-14 04:13:09.0

1、IDEA中新建项目

2、选择依赖:

3、next、finish 项目创建完毕(项目名、groupid等信息自己根据喜好定)

4、创建controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class MyThymeleafController {@RequestMapping("/hello")public String hello(Model model){model.addAttribute("msg", "HELLO");System.out.println("hello开始");return "hello";}
}

注意:这里使用的是@Controller 而不是@RestController。 如果使用RestController,hello方法返回的是字符串,会在页面直接打印字符串hello。使用Controller,会根据返回的字符串找对应的html文件

5、在resources/templates目录下创建hello.html,文件名必须对应controller中返回的名称

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div th:text="${msg}"></div><form action="" method="post"><form action="" method="post"><label th:text="#{login.username}">Username</label><input type="text"  name="username"  placeholder="Username" th:placeholder="#{login.username}"><label th:text="#{login.password}">Password</label><input type="password" name="password" placeholder="Password" th:placeholder="#{login.password}"><br> <br><div><label><input type="checkbox" value="remember-me"/> [[#{login.remmber}]]</label></div><br><button  type="submit" th:text="#{login.sign}">Sign in</button><br> <br><a th:href="@{/hello(language='zh_CN')}">中文</a><a th:href="@{/hello(language='en_US')}">English</a></form></form>
</body>
</html>

6、到这里,启动服务可以测试一下,访问localhost:8080/hello,可以访问到hello.html页面了。但是国际化还没有搞,接下来搞国际化。

7、在resources下面建 i18n 目录,然后在i18n目录下,建lang.properties, lang_zh_CN.properties, 建完之后,idea会自动转化为国际化目录,如图:

然后在i18n目录下再建一个lang_en_US.properties。

内容如下:
#login.properties
login.password=密码1
login.remmber=记住我1
login.sign=登录1
login.username=用户名1
#login_en_US.properties
login.password=Password
login.remmber=Remember Me
login.sign=Sign In
login.username=Username
#login_zh_CN.properties
login.password=密码~
login.remmber=记住我~
login.sign=登录~
login.username=用户名~

8、建一个WebMvcConfigurer实现类注册自己的LocaleResolver


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.WebMvcConfigurer;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/hello").setViewName("hello");
//    }@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}private static class MyLocaleResolver implements LocaleResolver{@Overridepublic Locale resolveLocale(HttpServletRequest request) {String language = request.getParameter("language"); //对应前端语言切换传的参数名System.out.println("当前语言:"+ language);Locale locale = Locale.getDefault();if(!StringUtils.isEmpty(language)){String[] split = language.split("_");locale = new Locale(split[0],split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {}}}

9、在resources目录下建application.yaml文件,然后删除application.properties。(也可以直接使用applcation.properties,个人习惯问题。applcation.yaml对应的字符集默认是utf-8,而application.properties默认是iso8859-1)。

在application.yaml文件中添加i18n配置

spring:messages:basename: i18n/lang

10、在File-setting-Editor-File Encodings中将encoding改为utf-8,应用-ok

 

至此,一个简单的springboot使用thymeleaf以及实现国际化的例子就完了。

Thymeleaf是一个Java库。它是一个基于XML/XHTML/HTML5的模板引擎,能够对模板文件执行一组变换从而使其能够展现应用生成的数据和文本,其自定义实现了一系类th标签。

具体细节可以参考一个Thymeleaf的中文网站:

中文网站:https://raledong.gitbooks.io/using-thymeleaf/content/