---
title: "OpenJWeb快速开发平台使用struts2实现多语切换的方法"
author: "cjc"
date: "2009-04-07T09:29:00Z"
tags: []
slug: "openjweb-struts2"
draft: false
lastmod: "2009-04-07T09:29:00Z"
id: 446849
url: "https://cjc.iteye.com/blog/446849"
---

页面中设置多语言连接:

&lt;a href="&lt;%=request.getContextPath()%&gt;/comm/lang!changeLang.action?lang=zh-CN"&gt; &lt;font color="#FF0000"&gt;中文&lt;/font&gt; &lt;/a&gt;  
&nbsp;&nbsp;&nbsp;&lt;a href="&lt;%=request.getContextPath()%&gt;/comm/lang!changeLang.action?lang=en-US" &gt;&lt;font color="#00FF00"&gt;English&lt;/font&gt;&lt;/a&gt;  
&nbsp;&nbsp;&nbsp;&lt;a href="&lt;%=request.getContextPath()%&gt;/comm/lang!changeLang.action?lang=ja-JP""&gt;&lt;font color="#0000FF"&gt;日本語&lt;/font&gt;&lt;/a&gt;  
&nbsp;&nbsp;&nbsp;&lt;a href="&lt;%=request.getContextPath()%&gt;/comm/lang!changeLang.action?lang=ko-KR""&gt;&lt;font color="#FFFF00"&gt;한국어&lt;/font&gt;&lt;/a&gt;

在action中根据传入的参数设置session实现多语切换:

public String changeLang()  
{  
if(this.lang!=null)  
{  
logger.info(this.getLang());  
}  
else  
logger.info("语言种类为空!");  
//String sLocale=request.getSession().getAttribute("org.apache.struts.action.LOCALE").toString();  
//  
if(lang==null||lang.trim().length()==0)  
request.getSession().setAttribute("WW\_TRANS\_I18N\_LOCALE",java.util.Locale.CHINA); //默认设置为中文  
if(lang.equals("zh-CN"))request.getSession().setAttribute("WW\_TRANS\_I18N\_LOCALE",java.util.Locale.CHINA);  
if(lang.equals("en-US"))request.getSession().setAttribute("WW\_TRANS\_I18N\_LOCALE",java.util.Locale.US);  
if(lang.equals("ko-KR"))request.getSession().setAttribute("WW\_TRANS\_I18N\_LOCALE",java.util.Locale.KOREA);  
if(lang.equals("ja-JP"))request.getSession().setAttribute("WW\_TRANS\_I18N\_LOCALE",java.util.Locale.JAPAN);

//logger.info(request.getSession().getAttribute("WW\_TRANS\_I18N\_LOCALE").toString() );  
//logger.info(request.getSession().getAttribute("WW\_TRANS\_I18N\_LOCALE").getClass().getName());  
//logger.info("---"+sLocale+"---------");  
//if(this.getLang()!=null&&this.getLang().equals(""))  
//{

//}

return "lang";  
}