SpringBoot使用Thymeleaf配置国际化页面

原生目录:

1.在pom.xml文件中添加Thymeleaf相关依赖

<!--SpringBoot父项目依赖管理-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--导入 spring-boot-starter-web-->
    <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.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.7.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-test</artifactId>
      <version>2.3.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>

    <!--设置项目热部署——自动更新-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
      <scope>true</scope>
    </dependency>
    <!--Thymeleaf 启动器-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>

使用插件

<plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <!--热部署配置-->
          <configuration>
            <!--fork:如果没有该项配置,整个devtools不会起作用-->
            <fork>true</fork>
          </configuration>
        </plugin>

2.在resources中创建Springboot全局配置文件application.properties,用来配置

Thymeleaf模板的参数以及添加国际化文件的基础名。

#thymeleaf页面缓存设置(默认为true),开发中方便调试应设置为false,
#上线稳定后应保持默认true
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
#配置国际化文件基础名
spring.messages.basename=i18n.login

3.在resources中创建i18n文件夹,编写多语言国际化文件。并在该目录下创建login.properties、login_zh_CN.properties、login_en_US.properties三个文件。

(1)login.properties

login.tip=请登录
login.username=用户名
login.password=密码
login.rememberme=记住我
login.button=登录

(2)login_zh_CN.properties

login.tip=请登录
login.username=用户名
login.password=密码
login.rememberme=记住我
login.button=登录

(3)login_en_US.properties

login.tip=Please sign in
login.username=Username
login.password=Password
login.rememberme=Rememberme
login.button=Login

4.在org.example下创建config包,在包下新建MyLocalResovel类,并且继承LocaleResolver。

package org.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

@Configuration
public class MyLocalResovel implements LocaleResolver {
    //自定义区域解析器
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取页面手动切换传递的语言参数l
        String l = request.getParameter("l");
        //获取请求头自动传递的语言参数Accept-Language
        String header = request.getHeader("Accept-Language");
        Locale locale = null;
        /*
         * 如果手动切换参数不为空,
         * 就根据手动参数进行语言切换,
         * 否则默认根据请求头信息切换
         */

        if(!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            locale = new Locale(split[0],split[1]);
        }else{
            //Accept-Language:en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
            String[] splits=header.split(",");
            String[] split=splits[0].split("-");
            locale = new Locale(split[0],split[1]);

        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {

    }
    //将自定义的MyLocalResovel类重新注册为一个类型LocaleResolver的Bean组件
    @Bean
    public MyLocalResovel localeResolver(){
        return new MyLocalResovel();
    }
}

5.创建controller包,并新建LoginController类作web控制器

package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;

@Controller
public class LoginController {
    /*
     *获取并封装当前年份跳转到登录页login.html*parammodel
     *@return
     */
    @RequestMapping("/toLoginPage")
    public String toLoginPage(Model model){
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
}

6.创建模板页面login.html并引入静态资源文件bootstrap.min.css与signin.css

<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
    <title>用户登录界面</title>
    <link th:href="@{/login/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/login/css/signin.css}" rel="stylesheet">

</head>
<body class="text-center">
<!--用户登录form表单-->
<form class="form-signin">
    <img class="mb-4" th:src="@{/login/img/login.jpg}" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">请登录</h1>
    <input type="text" class="form-control" th:placeholder="#{login.username}"required="" autofocus="">
    <input type="password" class="form-control" th:placeholder="#{login.password}" required="">
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me">[[#{login.rememberme}]]
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">登录</button>
    <p class="mt-5 mb-3 text-muted">
        <span th:text="${currentYear}">2018</span>-<span th:text="${currentYear}+1">2019</span>
    </p>
    <a class="btn btn-sm" th:href="@{/toLoginPage(l='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/toLoginPage(l='en_US')}">English</a>
</form>
</body>
</html>

7.启动类SpringbootApplication

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootAppliction {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootAppliction.class, args);
    }
}

8.效果截图,运行启动类SpringbootApplication,然后在浏览器中打开地址:

http://localhost:8080/toLoginPage

中文模式

英文模式

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值