SpringBoot(10)web开发之样式生效和国际化

样式生效

html是由thyemeleaf来接管,使样式生效,使用th:src="@{/img/bootstrap-solid.svg}"来代替原先样式。

国际化

1.在资源下创建i18n目录,配置好需要转换得语言配置文件
英文

login.btn=Sign in
login.password=Password
login.remeber=Remerber me
login.tip=Please sign in
login.username=Username

中文

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

2.语言切换按钮给予参数,根据参数来实现中英文转换。

			//对文本内容进行替换
			<input type="password" class="form-control" th:placeholder="#{login.password}" required="">
			<div class="checkbox mb-3">
			<label>
			//对按钮进行内容替换
         	 <input type="checkbox" value="remember-me" > [[#{login.remeber}]]
       		</label>
			<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
			<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>

3.创建自定义国际化组件

public class MyLocaleResolver implements LocaleResolver {
    //请求绑定
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取请求中得参数
        String language = httpServletRequest.getParameter("l");
        //使用默认
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(language)){
            String[] split = language.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

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

    }


}

4.将组件注册到容器中


@Configuration
public class MyMVCConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
    //自定义国际化组件注册
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值