基于Session的國際化實(shí)現(xiàn)方法
如何將我們網(wǎng)站的其它內(nèi)容(如菜單、標(biāo)題等)做國際化處理呢?這就是本篇要將的內(nèi)容—>國際化。
在項(xiàng)目的spring.xml文件添加的內(nèi)容如下
<mvc:interceptors> <span style="white-space:pre"> </span><!-- 國際化操作攔截器 如果采用基于(請求/Session/Cookie)則必需配置 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors>
在項(xiàng)目中的源文件夾resources中添加myproperties.properties、myproperties_zh_.properties、myproperties_en_.properties三個文件

下面是jsp頁面的一些簡單信息如下,僅僅是演示沒考慮其他的:
<%@ 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>
后臺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)的效果圖展示大家測試一下就好了!寫的不好的地方希望大家批評指正。
以上這篇基于Session的國際化實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)例解決Java異常之OutOfMemoryError的問題
在本篇文章中,我們給大家分享了關(guān)于解決Java異常之OutOfMemoryError的問題的方法,有此需要的朋友們學(xué)習(xí)下。2019-02-02
SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場景分析
這篇文章主要介紹了SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場景分析,本篇文章簡單介紹?OpenFeign?調(diào)用傳遞?header?,以及多線程環(huán)境下可能會出現(xiàn)的問題,其中涉及到?ThreadLocal?的相關(guān)知識,需要的朋友可以參考下2022-07-07
詳解Java的JDBC API的存儲過程與SQL轉(zhuǎn)義語法的使用
這篇文章主要介紹了詳解Java的JDBC API的存儲過程與SQL轉(zhuǎn)義語法的使用,JDBC是Java用于連接使用各種數(shù)據(jù)庫的API,需要的朋友可以參考下2015-12-12

