springBoot实现国际化

1.创建国际化配置文件

2.配置简单的属性(使用idea时点击Resource Bundle即可进入资源捆绑编辑模式,点击加号就可以在不同的配置文件中添加相同属性字段,最右边输入值,相对应的配置文件就会给属性赋值)

3.修改默认的国际化文件路径(spring的默认路径是message文件下)

4.搭建一个html页面(html标签内一定加上(xmlns:th="http://www.thymeleaf.org")这个;然后使用th:placeholder="#{login.id_prompt_prompt} 获取配置文件中的值)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登陆页面</title>
    <link th:href="@{/layui/css/layui.css}" rel="stylesheet"/>
    <script th:src="@{/jquery/jquery-3.6.0.js}"></script>
    <script th:src="@{/layui/layui.js}"></script>
</head>
<body style="background-color:#02608f;">
<div class="layui-fluid">
    <div class="layui-col-xs12" style="float: initial;">
        <a th:href="@{/login(l='zh_CN')}" style="color: #eee;">中文 |</a>
        <a th:href="@{/login(l='en_US')}" style="color: #eee;">English</a>
    </div>
    <div id="Login_div" class="layui-row">
        <form id="formLogin" class="layui-row layui-form" method="post" action="${ctx}/LoginServlet?fun=userLogin">
            <div class="layui-col-xs12" id="login_form">
                <div class="layui-col-xs11">
                    <div class="layui-form-item ">
                        <label class="layui-form-label" style="color: #359ed4" th:text="#{login.user}"></label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" th:placeholder="#{login.id_prompt_prompt}"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
                <i class="layui-col-xs12" style="border-bottom: 1px solid #d9d9d9;margin-bottom: 15px"></i>
                <div class="layui-col-xs11">
                    <div class="layui-form-item ">
                        <label class="layui-form-label" style="color: #359ed4" th:text="#{login.password}"></label>
                        <div class="layui-input-block">
                            <input type="password" name="password" th:placeholder="#{login.password_prompt}"
                                   autocomplete="off"
                                   class="layui-input" value="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12" align="center" style="margin-top: 5px">
                <button type="button" class="layui-btn" onclick="ringUp()" id="register_btn">登录
                </button>
            </div>
        </form>

    </div>
    <div class="layui-col-xs4" style="margin: 0;padding: 0">&nbsp;</div>
</div>
<style>
    form p {
        margin: 5px 0;
    }

    form p span {
        text-align: right;
        padding-right: 10px;
    }

    .layui-btn {
        height: 50px;
        line-height: 50px;
        font-size: 24px;
        padding: 0 40px;
    }

    .layui-input {
        height: 38px;
        line-height: 1.3;
        line-height: 38px;
        border-width: 1px;
        border-style: solid;
        background-color: #fff;
        border-radius: 5px;
    }

    #Login_div {
        border-radius: 15px;
        opacity: 0.7;
        margin: 0px auto;
        padding: 10px;
        width: 400px;
        background-size: 100% 100%;
        background-color: #196998;
    }

    #login_form {
        background-color: #e7e7e7;
        border-radius: 15px;
        padding: 5px;
        padding-top: 15px;
        background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#fff), to(#ddd));
    }

    #register_btn {
        background-color: #0993d1;
        border-radius: 15px;
        width: 100%;
    }
</style>
<script>
    //layui插件模块变量
    var layer, layui_form;
    //背景图的大小伸缩变量
    /**
     * windowHeight 浏览器窗口高度
     * thisHeight 登录盒子高度
     * heightBad 两者差值
     */
    var windowHeight, thisHeight, heightBad;
    if (window.top !== window.self) {
        window.top.location = window.location;
    }
    $(function () {
        windowHeight = window.innerHeight;
        thisHeight = $("#Login_div").height();
        heightBad = ((windowHeight - 200 - thisHeight) / 2) + "px";
        $("#Login_div").css("margin-top", heightBad)
        $(window).resize(function () {
            windowHeight = window.innerHeight;
            thisHeight = $("#Login_div").height();
            heightBad = ((windowHeight - 200 - thisHeight) / 2) + "px";
            $("#Login_div").css("margin-top", heightBad)
        })
        //回车登录
        $('#search_input').bind('keyup', function (event) {
            if (event.keyCode == 13) {
                ringUp();
            }
        })
        //layui定义
        layui.use(['layer', 'form'], function () {
            layer = layui.layer;
            layui_form = layui.form;
        });
    })

    //登录点击事件
    function ringUp() {
        var layerIndex = layer.load();
        var name = $("input[name='name']").val();
        var password = $("input[name='password']").val();
        if (name != "" && password != "") {
            $.post("${ctx}/login/loginCheck", {
                name: name,
                password: password,
            }, function (data) {
                //输出下data的数据类型
                var retdata = JSON.parse(data);
                if (retdata.state) {
                    layer.msg("登录成功", {icon: 1, title: "提示"});
                    setTimeout(function () {
                        window.location.href = "${ctx}/login/loginView";
                    }, 0);
                } else {
                    layer.alert("登录失败:" + retdata.msg, {icon: 2, title: "提示"})
                }
                //关闭弹出层
                layer.close(layerIndex)
            })
        } else {
            layer.msg("填写完整", {icon: 2});
            //关闭弹出层
            layer.close(layerIndex)
        }
    }
</script>
</body>
</html>

5.创建自定义的视图解析器和地区解析器

6.编写视图解析器

package com.tao.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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//@EnableWebMvc//导入了一个类(DelegatingWebMvcConfiguration:从spring容器中获取所有webMvcConfiguration),添加这个注解会使spring默认配置失效
//myMvcConfig:自定义的视图解析器
public class myMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //addViewController:设置请求路径;setViewName:返回的视图名称
        registry.addViewController("/").setViewName("test");
        registry.addViewController("/login").setViewName("login");
    }
}

7.编写地区解析器

package com.tao.config;

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;

public class MyLocaleResolver implements LocaleResolver {
    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取请求中的参数链接
        String language = request.getParameter("l");//前端传递的参数用来判断中文还是英文

        Locale locale = Locale.getDefault();//获取默认Locale

        if (!StringUtils.isEmpty(language)) {//判断传递参数是否为空
            //zh_CN
            String[] split = language.split("_");//前后分割 分开获取国家和地区
            //国家,地区
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }
    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }}

8.注册自定义地区解析器交给spring容器保管

 @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

6.提示

注意:地区解析器的bean一定是在视图解析器中注册,否则会失效.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值