spring-boot之国际化

1.-自定义国际化语言解析器

package com.atguigu.springbootweb.component;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

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

/**
 * 作者:
 * 创建时间:2019/6/30
 * 项目名字:com.atguigu.springbootweb.component
 * 版本:1.0
 **/

//发送请求的时候带上语言参数
public class MycoaleResolver implements LocaleResolver {

//    这样切换语言的BUG    在浏览器切换语言不管用了

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l=request.getParameter("l");
        //如果参数中不带 l() 参数,就用默认的L,带的话就用参数中的语言信息
        Locale locale=Locale.getDefault();
        if(!StringUtils.isEmpty(l))
        {
            String [] split1=l.split("_");
            locale=new Locale(split1[0],split1[1]);
            HttpSession session = request.getSession();
            session.setAttribute(l,locale);
        }
        return locale;
    }

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

    }
}

2.创建配置类,将组件加入容器中去

 

package com.atguigu.springbootweb.config;

import com.atguigu.springbootweb.component.MycoaleResolver;
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.WebMvcConfigurerAdapter;

/**
 * 作者:
 * 创建时间:2019/6/30
 * 项目名字:com.atguigu.springbootweb.config
 * 版本:1.0
 **/

//WebMvcConfigurationSupport  来扩展springmvc的功能,但是自动配置失效,继承WebMvcConfigurerAdapter,
//    自动配置不会失效

//@EnableWebMvc//全面接管SpringMvc
@Configuration//此注释是一个配置类
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        //意思时浏览器发送硅谷请求也来到success请求(以后一个静态页面的话可以使用这种方式)
        registry.addViewController("/guigu").setViewName("success");
    }

    //将组建注册在容器中(如果咩有bean,会访问public文件夹下的index.html)
    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter dapter =new WebMvcConfigurerAdapter(){
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                 registry.addViewController("/").setViewName("login");
                registry.addViewController("/login.html").setViewName("login");
            }
        };
        return dapter;
    }

//    将写好的语言组件加入容器中,才能生效(LocaleResolver)
    @Bean(name = "localeResolver")//value时必须的
    public LocaleResolver getMycoaleResolver(){
        return new MycoaleResolver();
    }
}

3.界面参数中加入语言信息

<!DOCTYPE html>
<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">
		<meta name="description" content="">
		<meta name="author" content="">
		<title>Signin Template for Bootstrap</title>
		<!-- Bootstrap core CSS -->
		<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.css}" rel="stylesheet">
		<!-- Custom styles for this template -->
		<link href="asserts/css/signin.css" th:href="@{asserts/css/signin.css}"  rel="stylesheet">
	</head>

	<body class="text-center">
		<form class="form-signin" action="dashboard.html">
			<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
			<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
			<label class="sr-only" th:text="#{login.userName}" >Username</label>
			<input type="text" class="form-control" placeholder="Username" required="" autofocus="">
			<label class="sr-only" th:text="#{login.passworld}">Password</label>
			<input type="password" class="form-control" placeholder="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" type="submit"th:text="#{login.sign}">Sign in</button>
			<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
			<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
			<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
		</form>

	</body>

</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值