日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springboot+jsp中文乱码_【spring 国际化】springMVC、springboot国际化处理详解

發(fā)布時(shí)間:2023/12/19 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot+jsp中文乱码_【spring 国际化】springMVC、springboot国际化处理详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在web開發(fā)中我們常常會(huì)遇到國際化語言處理問題,那么如何來做到國際化呢?

你能get的知識(shí)點(diǎn)?

  • 使用springgmvc與thymeleaf進(jìn)行國際化處理。
  • 使用springgmvc與jsp進(jìn)行國際化處理。
  • 使用springboot與thymeleaf進(jìn)行國際化處理。
  • 目錄

    • 一:使用springgmvc與thymeleaf進(jìn)行國際化處理。
    • 二: 使用springgmvc與jsp進(jìn)行國際化處理。
    • 三:使用springboot與thymeleaf進(jìn)行國際化處理。

    你必須要知道的概念

    關(guān)于i18n:

    i18n(其來源是英文單詞

    internationalization的首末字符i和n,18為中間的字符數(shù))是“國際化”的簡稱。在資訊領(lǐng)域,國際化(i18n)指讓產(chǎn)品(出版物,軟件,硬件等)無需做大的改變就能夠適應(yīng)不同的語言和地區(qū)的需要。對(duì)程序來說,在不修改內(nèi)部代碼的情況下,能根據(jù)不同語言及地區(qū)顯示相應(yīng)的界面。

    在全球化的時(shí)代,國際化尤為重要,因?yàn)楫a(chǎn)品的潛在用戶可能來自世界的各個(gè)角落。通常與i18n相關(guān)的還有L10n(“本地化”的簡稱)。

    一:使用springgmvc與thymeleaf進(jìn)行國際化處理。

    1、在項(xiàng)目spring的Spring MVC配置文件springmvc.xml中,你需要配置

    • 資源文件綁定器ResourceBundleMessageSource
    • SessionLocaleResolver(用于將Locale對(duì)象存儲(chǔ)于Session中供后續(xù)使用)
    • LocaleChangeInterceptor(用于獲取請(qǐng)求中的locale信息,將其轉(zhuǎn)為Locale對(duì)象,獲取LocaleResolver對(duì)象)。
    <!-- 使用ResourceBundleMessageSource實(shí)現(xiàn)國際化資源--><bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basenames" value="messages"/><property name="defaultEncoding" value="UTF-8"/></bean><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"><property name="defaultLocale" value="en_US"/></bean><mvc:interceptors><bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"><property name="paramName" value="lang"/></bean></mvc:interceptors>

    2、在控制器中添加方法localeChange處理國際化,并注入ResourceBundleMessageSource的Bean實(shí)例

    @Autowiredprivate ResourceBundleMessageSource messageSource;@GetMapping("/localeChange")public String localeChange(Locale locale){String userName = messageSource.getMessage("userName",null,locale);String passWord = messageSource.getMessage("passWord",null,locale);System.out.println(userName+passWord);return "login";}

    3、創(chuàng)建國際化資源屬性文件 messages_en_US.properties 和 messages_zh_CN.properties 。
    注意這兩個(gè)文件的命名格式,否則解析會(huì)出錯(cuò),
    并且我這里的兩個(gè)文件就是位于我的resources目錄下,當(dāng)你新建這兩個(gè)文件后,他會(huì)自動(dòng)給你歸檔,不要以為我的這兩個(gè)上面還有一層,你也跟著建一個(gè)文件夾。

    1、messages_en_US.properties userName=userName passWord=password2、messages_zh_CN.properties userName=用戶名 passWord=密碼

    4、新建html,用于最終的顯示,這里使用的是thymeleaf模板引擎,沒有做springmvc與thymeleaf的整合可以看我的另一篇文章 springmvc與thymeleaf的整合

    <!--@Author: lomtom@Date: 2020/4/19@Time: 16:51@Email: lomtom@qq.com--><!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>login</title> </head> <body> <h2 th:text="#{userName}+' '+#{passWord}"></h2> <a href="localeChange?lang=en_US">英文</a> <a href="localeChange?lang=zh_CN">中文</a> </body> </html>

    最終的效果:

    二: 使用springgmvc與jsp進(jìn)行國際化處理。

    這里的前三部與上面相同,唯一有區(qū)別的就是最終視圖的顯示,使用jsp,就要用到JSTL標(biāo)簽,這里需要引入JSTL的message標(biāo)簽。

    即在頭部加入 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

    然后通過 <spring:message> 元素的key屬性輸出資源屬性文件中的key所對(duì)應(yīng)的值,最終的jsp是這樣的。

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html> <head><title>login</title> </head> <body><p><spring:message code="userName"/></p><p><spring:message code="password"/></p><a href="localeChange?lang=en_US">英文</a><a href="localeChange?lang=zh_US">中文</a> </body> </html>

    依次點(diǎn)擊“中文”和“英文”鏈接,可以看到 <spring:message> 元素顯示的文本能夠根據(jù)所傳遞的語言來動(dòng)態(tài)展現(xiàn)。

    三:使用springboot與thymeleaf進(jìn)行國際化處理。

    沒有做springmvc與thymeleaf的整合可以看我的另一篇文章 springmvc與thymeleaf的整合

    1、創(chuàng)建配置文件,抽取頁面要顯示的消息

    2、springboot已經(jīng)配置好了組件,加入即可

    spring: # 讓springboot來管理配置文件messages:basename: i18n.login

    3、讓頁面獲取即可(默認(rèn)根據(jù)瀏覽器語言來切換),利用 thymeleaf

    <button type="submit" class="btn btn-default" th:text="#{login.btn}">Sign in</button>

    4、根據(jù)鏈接來進(jìn)行語言的切換

    原理:國際化locale(區(qū)域信息對(duì)象),如果自定義了,就是用自己的,而不是系統(tǒng)的

    <a class="btn btn-sm" th:href="@{/index.html(l=zh_CN)}">[[#{login.zh}]]</a> <a class="btn btn-sm" th:href="@{/index.html(l=en_US)}">[[#{login.en}]]</a> <a class="btn btn-sm" th:href="@{/index.html(l=kor_KR)}">[[#{login.kor}]]</a>

    5、解析器,因?yàn)槲覀冊(cè)O(shè)置了自己的區(qū)域信息對(duì)象,所以我們需要書寫自己的解析器。并且注入到容器中。

    1、新建一個(gè)LocaleResolver public class MyLocaleResolver implements LocaleResolver {@Override//解析區(qū)域信息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;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {} }2、在config文件中加入到容器中 @Configuration public class MyMvcConfig implements WebMvcConfigurer {@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();} }

    這個(gè)寫的時(shí)間過于久遠(yuǎn),所以貼出代碼,感興趣的,可以自己研究研究:

    1、html

    <!--User: 歐陽隆桐Date: 2019/12/22Time: 16:42--> <!DOCTYPE html> <html lang="ch" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>[[#{login.title}]]</title><link rel="stylesheet" href="/webjars/bootstrap/4.4.1/css/bootstrap.css" th:href="@{/webjars/bootstrap/4.4.1/css/bootstrap.css}"><script src="/webjars/jquery/3.3.1/jquery.js" th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script><script src="/webjars/bootstrap/4.4.1/js/bootstrap.js" th:src="@{/webjars/bootstrap/4.4.1/js/bootstrap.js}"></script> </head> <body> <div class="container"><div class="row clearfix"><div class="col-md-12 column"><form class="form-horizontal" role="form" th:action="@{/user/login}" method="post"><div class="form-group text-center"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1><p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p></div><div class="form-group"><label for="username" class="col-sm-2 control-label" th:text="#{login.username}">Username</label><div class="col-sm-10"><input type="text" class="form-control" name="username" id="username" /></div></div><div class="form-group"><label for="password" class="col-sm-2 control-label" th:text="#{login.password}">Password</label><div class="col-sm-10"><input type="password" class="form-control" name="password" id="password" /></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><div class="checkbox"><label><input type="checkbox" />[[#{login.remember}]]</label></div></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="submit" class="btn btn-default" th:text="#{login.btn}">Sign in</button></div></div><div class="form-group text-center"><p class="mt-5 mb-3 text-muted">@ 2019</p><a class="btn btn-sm" th:href="@{/index.html(l=zh_CN)}">[[#{login.zh}]]</a><a class="btn btn-sm" th:href="@{/index.html(l=en_US)}">[[#{login.en}]]</a><a class="btn btn-sm" th:href="@{/index.html(l=kor_KR)}">[[#{login.kor}]]</a></div></form></div></div> </div> </body> </html>

    2、properties

    1、默認(rèn) login.btn=登錄 login.en=英文 login.kor=韓文 login.password=密碼 login.remember=記住我 login.tip=請(qǐng)登錄 login.title=登錄 login.username=用戶名 login.zh=中文2、英文 login.btn=Sign in login.en=English login.kor=Korean login.password=Password login.remember=Remember me login.tip=Please sign in login.title=Login login.username=Username login.zh=Chinese3、韓文 login.btn=??? login.en=?? login.kor=?? login.password=?? login.remember=?? ????? login.tip=???????. login.title=??? login.username=??? ?? login.zh=???4、中文 login.btn=登錄 login.en=英文 login.kor=韓文 login.password=密碼 login.remember=記住我 login.tip=請(qǐng)登錄 login.title=登錄 login.username=用戶名 login.zh=中文

    3、java

    1、LocaleResolver /*** 可以在連接上攜帶區(qū)域信息*/public class MyLocaleResolver implements LocaleResolver {@Override//解析區(qū)域信息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;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {} }2、config @Configuration public class MyMvcConfig implements WebMvcConfigurer {@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();} }

    4、yml

    spring: # 讓springboot來管理配置文件messages:basename: i18n.login

    問題

    描述:顯示中文時(shí)亂碼
    解決:將國際化資源屬性文件的編碼格式設(shè)置為UTF-8即可,當(dāng)然也可以把整個(gè)項(xiàng)目編碼格式都設(shè)為UTF-8

    原文:

    【spring 國際化】springMVC、springboot國際化處理詳解?www.cnblogs.com

    總結(jié)

    以上是生活随笔為你收集整理的springboot+jsp中文乱码_【spring 国际化】springMVC、springboot国际化处理详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。