当前位置: 首页 > news >正文

山东网站建设找哪家网站优化排名推广

山东网站建设找哪家,网站优化排名推广,4399网页游戏官网,手机网站logospringboot的国际化也是基于spring mvc 的。 springboot 的 国际化消息支持–就是根据浏览器选择的语言,项目上的一些提示信息根据语言的选择进行对应的显示。 总结下国家化自动配置: 功能实现就是: 比如一个登录页面,我们在浏览…

springboot的国际化也是基于spring mvc 的。

springboot 的 国际化消息支持–就是根据浏览器选择的语言,项目上的一些提示信息根据语言的选择进行对应的显示。

总结下国家化自动配置:
功能实现就是:
比如一个登录页面,我们在浏览器选择语言是中国的,那么登录页面的所有提示消息就都是中文的。
如果浏览器语言选择英语,那么登录页面的所有提示消息就都是英文的。
而这些提示信息是写在 properties 配置文件中的。

★ 国际化的自动配置

Spring Boot为国际化自动配置提供了MessageSourceProperties类,该类负责读取有关国际化的配置属性

只有当指定basename的、默认的国际化资源文件(与语言、国家无关的国际化资源)存在时,Spring Boot的自动配置才会生效。比如配置 spring.messages.basename=mess,

那就意味着如果仅提供

appMess_zh_CN.properties(简体中文的资源文件)

appMess_en_US.properties(美式英语的资源文件),

关于国际化的自动配置不会生效;
只有当提供默认的 appMess.properties 文件时,国际化的自动配置才会生效。

代码演示:
需要有一个默认的才能生效。
在这里插入图片描述

★ 国际化的步骤

(1)编写国际化资源文件:basename_语言代码_国家代码.properties

(2)使用配置属性加载国际化资源文件。

Spring Boot只要使用简单的配置即可加载国际化资源文件。

 # 加载国际化资源文件spring.messages.basename=login_mess# 设置使用国际化消息的key作为默认消息spring.messages.use-code-as-default-message=true# 设置读取国际化消息的字符集是UTF-8spring.messages.encoding=UTF-8

(3)根据key输出国际化消息

分为两种情况:

 ▲ 在Java组件(如控制器等)中,直接使用容器内的MessageSource Bean(Spring Boot自动配置)的getMessage()方法获取国际化消息。▲ 在页面模板中,使用标签或表达式输出国际化消息。比如在Thymeleaf模板中,直接使用#{key}即可输出指定key对应的国际化消息。【对比】,要输出表达式的值,使用${}。【注意】:如果使用devtools来打包Spring Boot项目,最好将*.properties文件设为UTF-8字符集。

代码演示

需求:我们弄一个中文版和英文版的配置文件,在浏览器选择中国语言或者英语的时候,能相应的切换。

现在是什么都没配置的,看一下原始的情况。
创建一个项目,添加一个 index 登录页面,其他什么都没有。
然后查看常规的语言,也是默认的中国。所以是这样显示。

<h4 th:text="#{login_title}">首页</h4>
不理解为什么当 login_title 没值的时候,不是显示 “首页” 两个字

在这里插入图片描述

页面输出国际化信息的代码:
1、添加一个登录页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>国际化</title><!--  引入css样式,用 link 元素  ,  stylesheet 样式单 , .gz表示是打包的,但是springboot会自动解包 --><!--  引入 BootstrapWeb Jar 中的 CSS 样式  --><link rel="stylesheet" th:href="@{'/webjars/bootstrap/css/bootstrap.min.css'}"><!--  jquery 放在 bootstrap 前面,因为 bootstrap 需要依赖到 jquery  --><!--  引入 jQuery 的 Web Jar 中的 js 脚本  --><script type="text/javascript" th:src="@{'/webjars/jquery/jquery.min.js'}"></script><!--  引入 BootstrapWeb Jar 中的 js 脚本  --><script type="text/javascript" th:src="@{'/webjars/bootstrap/js/bootstrap.bundle.min.js'}"></script><!--  引入 popper 的 Web Jar 中的 Js 脚本  --><script type="text/javascript" th:src="@{'/webjars/popper.js/umd/popper.min.js'}"></script></head>
<body>
<div class="container"><h4 th:text="#{login_title}">首页</h4><div class="text-danger" th:if="${tip != null}" th:text="${tip}"></div><form method="post" th:action="@{/login}"><div class="form-group row"><label for="username" class="col-sm-3 col-form-label"th:text="#{name_label}">用户名</label><div class="col-sm-9"><input type="text" id="username" name="username"class="form-control" th:placeholder="#{'name_hint'}"></div></div><div class="form-group row"><label for="pass" class="col-sm-3 col-form-label"th:text="#{password_label}">密码</label><div class="col-sm-9"><input type="password" id="pass" name="pass"class="form-control" th:placeholder="#{'password_hint'}"></div></div><div class="form-group row"><div class="col-sm-6 text-right"><button type="submit" class="btn btn-primary"th:text="#{login_btn}">登录</button></div><div class="col-sm-6"><button type="reset" class="btn btn-danger"th:text="#{reset_btn}">重设</button></div></div></form>
</div>
</body>
</html>

2、
编写国际化资源文件:basename_语言代码_国家代码.properties
在这里插入图片描述

3、使用配置属性加载国际化资源文件。

Spring Boot只要使用简单的配置即可加载国际化资源文件。
要配置这些才能加载

#加载国际化资源文件
spring.messages.basename=appMess
#设置使用国际化消息的key作为默认消息
spring.messages.use-code-as-default-message=true
#设置读取国际化消息的字符集是 UTF-8
spring.messages.encoding=UTF-8
#设置消息的缓存时间--单位是秒
spring.messages.cache-duration=7200

在这里插入图片描述

可以看到这些属性已经能拿到配置类里面对应的值了。
在这里插入图片描述

测试:
当我们在浏览器切换语言的时候,这些页面会显示对应的文字,而且是我们自定义的文字。
在这里插入图片描述

controller 输出国际化信息的代码:

写一下controller 和domain
通过获取登录的用户名和密码,返回登录成功或失败的消息。
放回的这些消息是国际化的消息。就是要实现如果我在浏览器把默认语言改成英语,那么代码显示和消息也是英语。
在这里插入图片描述

配置添加登录成功和失败的国际化提示消息
在这里插入图片描述

前端取值的 # 和 $ 号。
在这里插入图片描述

完整代码:

application.properties

#加载国际化资源文件
spring.messages.basename=appMess
#设置使用国际化消息的key作为默认消息
spring.messages.use-code-as-default-message=true
#设置读取国际化消息的字符集是 UTF-8
spring.messages.encoding=UTF-8
#设置消息的缓存时间--单位是秒
spring.messages.cache-duration=7200

appMess.properties

这个配置啥也不用写,默认的,但必须有这个,国际化消息的功能才能生效

appMess_zh_CN.properties

login_title=登录页面
name_label=用户名
name_hint=请输入用户名
password_label=密码
password_hint=请输入密码
login_btn=登录
reset_btn=重设
#{0} 占位符
welcome={0}, 欢迎登录学习
failure=用户名和密码不匹配

appMess_en_US.properties

login_title=Login Page
name_label=User Name
name_hint=Please input User Name
password_label=Password
password_hint=Please input Password
login_btn=Login
reset_btn=Reset
#{0} 占位符
welcome={0}, Welcome to study
failure=sorry,password and username not matched

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>国际化</title><!--  引入css样式,用 link 元素  ,  stylesheet 样式单 , .gz表示是打包的,但是springboot会自动解包 --><!--  引入 Bootstrap 的 Web Jar 中的 CSS 样式  --><link rel="stylesheet" th:href="@{'/webjars/bootstrap/css/bootstrap.min.css'}"><!--  jquery 放在 bootstrap 前面,因为 bootstrap 需要依赖到 jquery  --><!--  引入 jQuery 的 Web Jar 中的 js 脚本  --><script type="text/javascript" th:src="@{'/webjars/jquery/jquery.min.js'}"></script><!--  引入 Bootstrap 的 Web Jar 中的 js 脚本  --><script type="text/javascript" th:src="@{'/webjars/bootstrap/js/bootstrap.bundle.min.js'}"></script><!--  引入 popper 的 Web Jar 中的 Js 脚本  --><script type="text/javascript" th:src="@{'/webjars/popper.js/umd/popper.min.js'}"></script></head>
<body>
<div class="container"><h4 th:text="#{login_title}">首页</h4><div class="text-danger" th:if="${tip != null}" th:text="${tip}"></div><form method="post" th:action="@{/login}"><div class="form-group row"><label for="username" class="col-sm-3 col-form-label"th:text="#{name_label}">用户名</label><div class="col-sm-9"><input type="text" id="username" name="username"class="form-control" th:placeholder="#{'name_hint'}"></div></div><div class="form-group row"><label for="password" class="col-sm-3 col-form-label"th:text="#{password_label}">密码</label><div class="col-sm-9"><input type="password" id="password" name="password"class="form-control" th:placeholder="#{'password_hint'}"></div></div><div class="form-group row"><div class="col-sm-6 text-right"><button type="submit" class="btn btn-primary"th:text="#{login_btn}">登录</button></div><div class="col-sm-6"><button type="reset" class="btn btn-danger"th:text="#{reset_btn}">重设</button></div></div></form>
</div>
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>登录成功</title><!--  引入css样式,用 link 元素  ,  stylesheet 样式单 , .gz表示是打包的,但是springboot会自动解包 --><!--  引入 Bootstrap 的 Web Jar 中的 CSS 样式  --><link rel="stylesheet" th:href="@{'/webjars/bootstrap/css/bootstrap.min.css'}"><!--  jquery 放在 bootstrap 前面,因为 bootstrap 需要依赖到 jquery  --><!--  引入 jQuery 的 Web Jar 中的 js 脚本  --><script type="text/javascript" th:src="@{'/webjars/jquery/jquery.min.js'}"></script><!--  引入 Bootstrap 的 Web Jar 中的 js 脚本  --><script type="text/javascript" th:src="@{'/webjars/bootstrap/js/bootstrap.bundle.min.js'}"></script><!--  引入 popper 的 Web Jar 中的 Js 脚本  --><script type="text/javascript" th:src="@{'/webjars/popper.js/umd/popper.min.js'}"></script></head>
<body>
<div class="container"><h4 th:text="${tip}">tip</h4>
</div>
</body>
</html>

LoginController

package cn.ljh.i18n.controller;import cn.ljh.i18n.domain.User;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;import java.util.Locale;@Controller
public class LoginController
{//依赖注入容器中自动配置 MessageSource Bean , 用于访问国际化信息private MessageSource messageSource;//构造器注入的方式public LoginController(MessageSource messageSource){this.messageSource = messageSource;}@PostMapping("/login")public String proLogin(User user , Model model , Locale locale){if (user.getUsername().equals("ljh") && user.getPassword().equals("123456")){//要添加国际化的提示信息//参数1:key   参数2:占位符  参数3:要确定国家地区和语言,用locale这个参数,springboot会自动揣摩model.addAttribute("tip",messageSource.getMessage("welcome" , new String[]{user.getUsername()} , locale));return "success";}//参数2:不需要占位符,直接给个 null 就行model.addAttribute("tip",messageSource.getMessage("failure" , null , locale));return "index";}}

User

@Data
public class User
{private Integer id;private String username;private String password;
}

文章转载自:
http://enumerative.rwzc.cn
http://restriction.rwzc.cn
http://wag.rwzc.cn
http://rapparee.rwzc.cn
http://anesthetize.rwzc.cn
http://sadist.rwzc.cn
http://ban.rwzc.cn
http://mirabilia.rwzc.cn
http://ryokan.rwzc.cn
http://hypertension.rwzc.cn
http://izba.rwzc.cn
http://polyglottery.rwzc.cn
http://collop.rwzc.cn
http://subaqueous.rwzc.cn
http://uninformative.rwzc.cn
http://sough.rwzc.cn
http://unfrequented.rwzc.cn
http://counterorder.rwzc.cn
http://san.rwzc.cn
http://atrium.rwzc.cn
http://standpipe.rwzc.cn
http://bluejacket.rwzc.cn
http://edmond.rwzc.cn
http://unconformable.rwzc.cn
http://evagination.rwzc.cn
http://moony.rwzc.cn
http://musca.rwzc.cn
http://stakhanovism.rwzc.cn
http://unmugged.rwzc.cn
http://sentinel.rwzc.cn
http://culling.rwzc.cn
http://norwards.rwzc.cn
http://commutable.rwzc.cn
http://venography.rwzc.cn
http://micrococcic.rwzc.cn
http://unglamorous.rwzc.cn
http://teleconference.rwzc.cn
http://mythical.rwzc.cn
http://landeshauptmann.rwzc.cn
http://counterpull.rwzc.cn
http://pentathlon.rwzc.cn
http://bonfire.rwzc.cn
http://erk.rwzc.cn
http://foa.rwzc.cn
http://piston.rwzc.cn
http://settle.rwzc.cn
http://thermoelectric.rwzc.cn
http://undated.rwzc.cn
http://theologaster.rwzc.cn
http://hypognathous.rwzc.cn
http://frozen.rwzc.cn
http://leaded.rwzc.cn
http://covary.rwzc.cn
http://headily.rwzc.cn
http://birdcage.rwzc.cn
http://corposant.rwzc.cn
http://balladmonger.rwzc.cn
http://refringent.rwzc.cn
http://curtsey.rwzc.cn
http://tactical.rwzc.cn
http://lineshaft.rwzc.cn
http://thwack.rwzc.cn
http://alkalosis.rwzc.cn
http://corrigent.rwzc.cn
http://gangly.rwzc.cn
http://belvedere.rwzc.cn
http://peepul.rwzc.cn
http://locarnize.rwzc.cn
http://enlarging.rwzc.cn
http://nonproficient.rwzc.cn
http://chozrim.rwzc.cn
http://chrysotile.rwzc.cn
http://extragalactic.rwzc.cn
http://hydronitrogen.rwzc.cn
http://univallate.rwzc.cn
http://extracurial.rwzc.cn
http://atelectasis.rwzc.cn
http://trenchancy.rwzc.cn
http://jade.rwzc.cn
http://insusceptibility.rwzc.cn
http://epifauna.rwzc.cn
http://unifactorial.rwzc.cn
http://sarmentum.rwzc.cn
http://cycloaddition.rwzc.cn
http://him.rwzc.cn
http://canephora.rwzc.cn
http://cowhand.rwzc.cn
http://istana.rwzc.cn
http://mcp.rwzc.cn
http://reforestation.rwzc.cn
http://aeroamphibious.rwzc.cn
http://drosera.rwzc.cn
http://restore.rwzc.cn
http://lati.rwzc.cn
http://switchyard.rwzc.cn
http://smog.rwzc.cn
http://cobalt.rwzc.cn
http://liquidly.rwzc.cn
http://bophuthatswana.rwzc.cn
http://amour.rwzc.cn
http://www.hrbkazy.com/news/81501.html

相关文章:

  • 做网站算运营吗长尾关键词举例
  • 贵阳做网站好的公司域名估价
  • 免费个人网站注册方法创意广告
  • 用什么网站做封面最好智慧软文发布系统
  • 备案网站容易被收录网络营销优秀案例
  • html5网站抓取网络优化工程师骗局
  • 医疗网站建设计划书优质友情链接
  • 苏州做外贸网站seo内部优化包括哪些内容
  • 深圳石岩做网站的公司山东seo推广公司
  • 国外做网站的软件如何查询百度收录情况
  • 手机产品展示网站模板武汉网站建设推广公司
  • 工商联网站建设方案友情链接平台站长资源
  • 做网站需要些什么资料seo百家论坛
  • 域名网站购买怎么搭建自己的网站
  • 手机如何建立网站平台常用的seo查询工具有哪些
  • 企业网站源码程序多少钱?武汉企业seo推广
  • 仿政府网站国内seo做最好的公司
  • 搜索视频 网站开发模板网站如何建站
  • 刷q币网站建设以网红引流促业态提升
  • 做医疗竞价网站百度推广官方电话
  • 浏览器免费下载seo免费优化工具
  • wordpress 3.8seochinaz查询
  • 怎样用html制作网站营销方式都有哪些
  • 搭建广告网站费用排名优化服务
  • 网站内容建设方法步骤链爱交易平台
  • 网站建设上市公司seo是什么意思知乎
  • 在线做网站怎么做百度关键词热搜
  • 做网站的难题南京百度seo代理
  • 为诈骗团伙做网站专业搜索引擎seo服务
  • 旅游企业做网站主要目的友情链接查询