Thymeleaf3 (三)国际化配置

建立一个登录页面

这个页面可以从bootstrap上下载。包括css页面和图标。

静态资源文件如下:

在templates目录下创建login.html

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="bootstrap.min.css" th:href="@{/bootstrap.min.css}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="signin.css" th:href="@{/signin.css}"  rel="stylesheet">
</head>

<body class="text-center">
<form class="form-signin">
    <img class="mb-4" src="bootstrap-solid.svg" th:src="@{/bootstrap-solid.svg}"  alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tips}">Please sign in</h1>
    <label for="inputEmail" class="sr-only" th:text="#{login.username}" >Email address</label>
    <input type="email" id="inputEmail" class="form-control" th:placeholder="#{login.username}" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only"  th:text="#{login.password}">Password</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" th:placeholder="#{login.password}"  required>
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me">[[#{login.remember}]]
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" th:text="#{login.btn}" type="submit">Sign in</button>

    <a class="btn btn-sm" th:href="@{/login(l='zh_CN')}"> 中文 </a>
    <a class="btn btn-sm" th:href="@{/login(l='en_US')}"> English </a>

    <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
</body>
</html>

在html中添加命名空间,不然代码自动提示不会生效。注意一下

<html xmlns:th="http://www.thymeleaf.org">

添加国际化资源文件

在application.properties中添加资源文件的路径

spring.messages.basename=i18n/login

为什么要配置?这是因为默认的配置文件是messages,在MessageSourceProperties文件中可以看到

public class MessageSourceProperties {
    private String basename = "messages";
    private Charset encoding;
    @DurationUnit(ChronoUnit.SECONDS)

login_zh_CN.properties

login.btn=登录
login.password=密码
login.remember=记住我
login.tips=请登录
login.username=邮件地址ַ

login_en_US.properties

login.btn=login
login.password=Password
login.remember=Remember me
login.tips=Please sign in
login.username=Email Address

创建loginController

package com.softtool.thymeleaf.controller;

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


@Controller
public class LoginController {

    @RequestMapping("/login")
    public String index(Model model){
        return  "/login";
    }
}

从WebMvcAutoConfiguration中可以看到,如果没有配置LocaleResolver(@ConditionalOnMissingBean),那么就会使用自动配置

        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(
            prefix = "spring.mvc",
            name = {"locale"}
        )
        public LocaleResolver localeResolver() {
            if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
                return new FixedLocaleResolver(this.mvcProperties.getLocale());
            } else {
                AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
                localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
                return localeResolver;
            }
        }

为实现跳转效果,需要在后台判断选择的语言,所以我们要实现LocaleResolver,而不是用默认的配置

package com.softtool.thymeleaf.component;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

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

public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        System.out.println("------------resolveLocale-----------");
        String l = httpServletRequest.getParameter("l");
        Locale aDefault = Locale.getDefault();
        if (!StringUtils.isEmpty(l)){
            String[] s = l.split("_");
            aDefault=  new Locale(s[0],s[1]);
        }
        return aDefault;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

package com.softtool.thymeleaf.config;


import com.softtool.thymeleaf.component.MyLocaleResolver;
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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class MyConfig extends WebMvcConfigurationSupport {
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");
        super.addResourceHandlers(registry);
    }
}

使用SpringBoot时遇到静态资源无法访问的问题,启用拦截器配置就会出现静态资源无法访问。 发现只要继承 WebMvcConfigurationSupport 并且将文件加入配置  发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源 ,所以从新写了addResourceHandlers方法

英文效果:

中文效果:

转载于:https://my.oschina.net/u/4006362/blog/2261507

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值