19、struts国际化的切换
2009-08-27 11:19
134 查看
在18、struts硬编码国际化的基础上,实现struts国际化的切换
1、了解利用struts默认将locale放到session中的特性,完成采用编程的方式切换语言设置
* 参见:ChangeLanguageAction.java
2、在index.jsp页面中增加
<a href="changelang.do?lang=zh">中文</a>
<a href="changelang.do?lang=en">英文<a/>3、ChageLanguageAction.java
Code
package com.bjsxt.struts;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ChangeLanguageAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//获得参数
String lang = request.getParameter("lang");
//得到当前的默认Locale
Locale currentLocale = Locale.getDefault();
if ("zh".equals(lang)) {
currentLocale = new Locale("zh", "CN");
}else if("en".equals(lang)) {
currentLocale = new Locale("en", "US");
}
//在Action中有setLocale方法来设置Locale
this.setLocale(request, currentLocale);
//也可以用使用如下方法
//request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
return mapping.findForward("index");
}
}
4、struts-config.xml加入配置信息
<action path="/changelang"
type="com.bjsxt.struts.ChangeLanguageAction"
>
<forward name="index" path="/index.jsp"/>
</action>
1、了解利用struts默认将locale放到session中的特性,完成采用编程的方式切换语言设置
* 参见:ChangeLanguageAction.java
2、在index.jsp页面中增加
<a href="changelang.do?lang=zh">中文</a>
<a href="changelang.do?lang=en">英文<a/>3、ChageLanguageAction.java
Code
package com.bjsxt.struts;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ChangeLanguageAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//获得参数
String lang = request.getParameter("lang");
//得到当前的默认Locale
Locale currentLocale = Locale.getDefault();
if ("zh".equals(lang)) {
currentLocale = new Locale("zh", "CN");
}else if("en".equals(lang)) {
currentLocale = new Locale("en", "US");
}
//在Action中有setLocale方法来设置Locale
this.setLocale(request, currentLocale);
//也可以用使用如下方法
//request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
return mapping.findForward("index");
}
}
4、struts-config.xml加入配置信息
<action path="/changelang"
type="com.bjsxt.struts.ChangeLanguageAction"
>
<forward name="index" path="/index.jsp"/>
</action>
相关文章推荐
- struts--动态访问国际化资源(国家/语言切换)
- struts多国语言国际化处理(现以中文英文切换为例)
- struts多国语言国际化处理(现以中文英文切换为例)
- struts国际化实例---登录例子(中英文切换)
- struts多国语言国际化处理(现以中文英文切换为例)
- STRUTS 2 国际化并自定义切换语言的问题解决
- 在Struts 2.0中国际化(i18n)您的应用程序
- Qt 国际化之二:多国语界面动态切换的实现
- 关于struts2+displaytag的国际化处理
- struts中文问题","struts国际化问题"——终极解决方案
- java语言国际化--ResouceBundle、struts
- Struts如何实现国际化
- Struts消息国际化及异常处理
- web,jsp,html网站中英文切换,资源国际化解决方案及代码(其中包含动态切换数据源及URL拦截器).
- Struts中的国际化
- 一个Struts实现分页,增删改查,Tiles,国际化的DEMO
- Qt多国语言的实现与切换(国际化)
- struts中文问题和国际化问题的终极解决方案
- "struts中文问题","struts国际化问题"——终极解决方案
- Struts2.0 国际化