咱们先来看一个html,带有css的,咱们就以这个为准来说解。html
资源能够去我网盘下载前端
连接:https://pan.baidu.com/s/1Elm1PgYLxSeWo0MyKSuiAA
提取码:lm6tjava
看看index.html里面的内容jquery
<!DOCTYPE html> <html lang="en"> <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" rel="stylesheet"> <!-- Custom styles for this template --> <link href="asserts/css/signin.css" rel="stylesheet"> </head> <body class="text-center"> <form class="form-signin" action="dashboard.html"> <img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"> <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1> <label class="sr-only">Username</label> <input type="text" class="form-control" placeholder="Username" required="" autofocus=""> <label class="sr-only">Password</label> <input type="password" class="form-control" placeholder="Password" required=""> <div class="checkbox mb-3"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2017-2018</p> <a class="btn btn-sm">中文</a> <a class="btn btn-sm">English</a> </form> </body> </html>
咱们能够看到这几个引用资源的web
<link href="asserts/css/bootstrap.min.css" rel="stylesheet"> <link href="asserts/css/signin.css" rel="stylesheet"> <img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
如今,咱们把这几个文件移到咱们的SpringBoot项目里面spring
个人index.html为了使用Thymeleaf的功能,放在了templates文件夹内,资源文件想引用,我放在了public文件夹下,由于Thymeleaf是不容许外部访问的bootstrap
如今咱们开始引用webjar了浏览器
什么是webjar?webjar就是一些前端的资源,好比Jquery,Bootstrap什么的,都打包成了jar包springboot
为何要使用webjar?由于单纯的使用jquery,bootstrap,多人协做开发的时候,可能会面临版本不一致的问题,以及放的位置不同的问题,为了解决这个问题,webjar出现了,你须要去webjar的网站找到maven引用,直接放在pom.xml里面就能够了,这样你也不用去下载了,也不用导入项目了。
使用Thymeleaf的功能,利用@{}
<link href="../asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet"> <link href="../asserts/css/signin.css" th:href="@{../asserts/css/signin.css}" rel="stylesheet"> <img class="mb-4" src="../asserts/img/bootstrap-solid.svg" th:src="@{../asserts/img/bootstrap-solid.svg }" alt="" width="72" height="72">
这样,就能够用了。
运行项目,是ok的(我如今忽然忘了,为何index.html在templates文件下也能访问了)
换成webjar以后,咱们的页面是这样的
能够看到,是英文的,可是咱们想要能够切换语言,有中文,英文的切换。这就是国际化
首先,咱们要新建国际化的配置文件,在resources文件下新建一个文件夹i18n,而后下面新建login.properties,再新建login_zh_CN.properties,你会发现,Idea自动识别了,给咱们建立了Resource Bundle 'login',咱们再新建一个en_US
这里有一个须要提醒的地方,先把你的Idea的文件格式设定为UTF-8,否则你写中文,会乱码,必定要先作这个!
注意:要先设置Idea的全部文件格式为UTF-8 |
随便点一个配置文件,点下面的第二个Resource Bundle,而后能够新建了,根据咱们页面上须要国际化的文字,有标题,用户名,密码,登陆按钮,记住信息等几个须要翻译的,咱们都写出来。
右边很直观,英文是咋样的,中文是咋样的,默认是咋样的。你就本身配置吧
配置完成以后,咱们使用一下国际配置文件
<!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.0.0/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" src="../asserts/img/bootstrap-solid.svg" th: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.passWord}">Password</label> <input type="password" class="form-control" placeholder="Password" required=""> <div class="checkbox mb-3"> <label> <input type="checkbox" value="remember-me" > [[#{login.RemeberMe}]] </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2017-2018</p> <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> </form> </body> </html>
这个是最终的文档类型,咱们使用了Thymeleaf的 th.text="#{login.userName}"这样的格式来调用咱们上面配置的,会有提示的。input标签不能使用 th:text,因此写成[[#{login.RemeberMe}]]
要在配置文件配置
spring: messages: basename: i18n/login
如今,咱们从新启动项目,根据更改浏览器的语言信息就发现,咱们已经能够切换语言了
如今,有一个新的需求,登陆页面下有两个汉字,中文和英文。
我但愿点击中文,就切换到中文。点击英文,就切换到英文。
想实现这个功能,就须要了解,中英文切换是根据Locale来决定的,咱们能够写一个类,来判断
首先,在页面的中英文切换这里,加上语言信息
<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>
而后新建一个判断的类
package com.vae.springboot.study.component; import org.springframework.context.annotation.Bean; 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 MyLocalResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest httpServletRequest) { String l = httpServletRequest.getParameter("l"); Locale locale = Locale.getDefault(); if (StringUtils.isEmpty(l)) { String [] split=l.split("_"); locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }
把这个类写到一个配置类里面去,其实这一点我不是太懂。留着之后看吧