基于Session的國(guó)際化實(shí)現(xiàn)方法
如何將我們網(wǎng)站的其它內(nèi)容(如菜單、標(biāo)題等)做國(guó)際化處理呢?這就是本篇要將的內(nèi)容—>國(guó)際化。
在項(xiàng)目的spring.xml文件添加的內(nèi)容如下
<mvc:interceptors> <span style="white-space:pre"> </span><!-- 國(guó)際化操作攔截器 如果采用基于(請(qǐng)求/Session/Cookie)則必需配置 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors>
在項(xiàng)目中的源文件夾resources中添加myproperties.properties、myproperties_zh_.properties、myproperties_en_.properties三個(gè)文件
下面是jsp頁(yè)面的一些簡(jiǎn)單信息如下,僅僅是演示沒(méi)考慮其他的:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <% Locale name = (Locale) session.getAttribute("i18nlanguage"); ResourceBundle myResourcesBundle = ResourceBundle.getBundle("myproperties",name); %> <body> <a href="${pageContext.request.contextPath}/index/findex.do?langType=en&page=Home">ENG</a> | <a href="${pageContext.request.contextPath}/index/findex.do?langType=zh&page=Home"><%=myResourcesBundle.getString("simplified")%></a> </body> </html>
后臺(tái)Action層代碼如下:
package com.zhidao.oms.index; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/index") public class IndexAction { @RequestMapping("/findex") public String Findex(HttpServletRequest request,@RequestParam String langType,String page){ if(langType.equals("zh")){ Locale locale = new Locale("zh", "CN"); request.getSession().setAttribute("i18nlanguage",locale); } else if(langType.equals("en")){ Locale locale = new Locale("en", "US"); request.getSession().setAttribute("i18nlanguage",locale); }else{ request.getSession().setAttribute("i18nlanguage",Locale.getDefault()); } return "/front/"+page+".jsp"; } }
有關(guān)的效果圖展示大家測(cè)試一下就好了!寫的不好的地方希望大家批評(píng)指正。
以上這篇基于Session的國(guó)際化實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
自定義mybatis插件如何實(shí)現(xiàn)sql日志打印
這篇文章主要介紹了自定義mybatis插件如何實(shí)現(xiàn)sql日志打印問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Spring中的singleton和prototype的實(shí)現(xiàn)
這篇文章主要介紹了Spring中的singleton和prototype的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07關(guān)于Java中Comparable 和 Comparator的用法
這篇文章主要介紹了關(guān)于Java中Comparable 和 Comparator的用法,Comparable 和 Comparator 是關(guān)于排序的兩個(gè)接口,用來(lái)實(shí)現(xiàn) Java 集合中的的排序功能,需要的朋友可以參考下2023-04-04elasticsearch kibana簡(jiǎn)單查詢講解
今天小編就為大家分享一篇關(guān)于elasticsearch kibana簡(jiǎn)單查詢講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02如何使用IDEA從SVN服務(wù)端檢出項(xiàng)目
這篇文章主要介紹了如何使用IDEA從SVN服務(wù)端檢出項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12簡(jiǎn)單易懂的MyBatis分庫(kù)分表方案分享
這篇文章主要給大家介紹了關(guān)于MyBatis分庫(kù)分表方案的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用MyBatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03JAVA 文件監(jiān)控 WatchService的示例方法
本篇文章主要介紹了JAVA 文件監(jiān)控 WatchService的示例方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10