基于Session的國際化實現(xiàn)方法
如何將我們網(wǎng)站的其它內(nèi)容(如菜單、標(biāo)題等)做國際化處理呢?這就是本篇要將的內(nèi)容—>國際化。
在項目的spring.xml文件添加的內(nèi)容如下
<mvc:interceptors> <span style="white-space:pre"> </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"; } }
有關(guān)的效果圖展示大家測試一下就好了!寫的不好的地方希望大家批評指正。
以上這篇基于Session的國際化實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的singleton和prototype的實現(xiàn)
這篇文章主要介紹了Spring中的singleton和prototype的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07關(guān)于Java中Comparable 和 Comparator的用法
這篇文章主要介紹了關(guān)于Java中Comparable 和 Comparator的用法,Comparable 和 Comparator 是關(guān)于排序的兩個接口,用來實現(xiàn) Java 集合中的的排序功能,需要的朋友可以參考下2023-04-04JAVA 文件監(jiān)控 WatchService的示例方法
本篇文章主要介紹了JAVA 文件監(jiān)控 WatchService的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10