基於Session的國際化完成辦法。本站提示廣大學習愛好者:(基於Session的國際化完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是基於Session的國際化完成辦法正文
若何將我們網站的其它內容(如菜單、題目等)做國際化處置呢?這就是本篇要將的內容—>國際化。
在項目標spring.xml文件添加的內容以下
<mvc:interceptors> <span > </span><!-- 國際化操作攔阻器 假如采取基於(要求/Session/Cookie)則必須設置裝備擺設 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors>
在項目中的源文件夾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";
}
}
有關的後果圖展現年夜家測試一下就行了!寫的欠好的處所願望年夜家批駁斧正。
以上這篇基於Session的國際化完成辦法就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。