JSF:中英文语言环境切换实例
2008-06-13 15:09
579 查看
几个关键的地方:
建立中文和英文的语言环境资源文件:
切换语言环境的链接,这里调用了FuncBean后台Bean的changLangEnv事件处理方法:
实现事件监听器,利用ActionEvent 的getComponent来获取事件源,利用FacesContext对象获取视图,并设置视图的语言环境,注意这里的语言常量(Locale.US和Locale.CHINESE):
jsf页面源码:
后台Bean(FuncBean)源码:
建立中文和英文的语言环境资源文件:
切换语言环境的链接,这里调用了FuncBean后台Bean的changLangEnv事件处理方法:
<h:form> <h:commandLink actionListener="#{FuncBean.changLangEnv}" value="中文" /> <h:commandLink actionListener="#{FuncBean.changLangEnv}" value="English" /> </h:form>
实现事件监听器,利用ActionEvent 的getComponent来获取事件源,利用FacesContext对象获取视图,并设置视图的语言环境,注意这里的语言常量(Locale.US和Locale.CHINESE):
public void changLangEnv(ActionEvent ae){ HtmlCommandLink hlink=(HtmlCommandLink)ae.getComponent(); String lang=(String)hlink.getValue(); if(lang.equals("English")){ FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.US); }else if(lang.equals("中文")){ FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.CHINESE); } }
jsf页面源码:
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%-- This file is an entry point for JavaServer Faces application. --%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <f:view> <f:loadBundle var="CusMes" basename="com.xgtimes.CustomMessages" /> <h1><h:outputText value="#{CusMes.appName}" /></h1> <hr /> <h:form> <h:commandLink actionListener="#{FuncBean.changLangEnv}" value="中文" /> <h:outputLabel value=" "/><h:outputLabel value=" "/> <h:commandLink actionListener="#{FuncBean.changLangEnv}" value="English" /> </h:form> <br /> <h:form> <h:inputText id="inputTxt"> <h:outputLabel value="#{CusMes.inputTxtLabel}"/> <f:validateLength minimum="3" maximum="10" /> </h:inputText> <h:commandButton id="okcom" type="submit" value="#{CusMes.okcomCaption}"></h:commandButton><br> <h:messages /> </h:form> </f:view> </body> </html>
后台Bean(FuncBean)源码:
package com.xgtimes; import java.util.Locale; import javax.faces.component.html.HtmlCommandLink; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; public class FuncBean { public FuncBean() { } public void changLangEnv(ActionEvent ae){ HtmlCommandLink hlink=(HtmlCommandLink)ae.getComponent(); String lang=(String)hlink.getValue(); if(lang.equals("English")){ FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.US); }else if(lang.equals("中文")){ FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.CHINESE); } } }
相关文章推荐
- Debian改变系统语言环境(中文切换到英文)实例
- Oracle11g切换语言环境
- struts国际化实例---登录例子(中英文切换)
- 静态网站的中英文等多语言切换
- Qt 界面实现多国语言的切换实例
- ILE 环境下的各种语言(C/C++, CL, RPG, COBOL)的混合编程实例及
- Windows 7 SP1 64bit 实施单机环境下DB2 V9.5.7 HADR实现实例级故障切换
- 切换Win10系统语言环境的方法
- 国际化模块 angular-translate 简单方便快捷翻译中英文等多语言环境
- 网站自动识别浏览器语言进行中英文切换的方法
- iOS 应用程序内切换语言 中英文切换
- Eclipse 中英文语言切换
- CAD编辑器的语言可以进行中英文切换吗?要怎么来操作呀?
- angular.js 国际化模块 angular-translate 简单方便快捷翻译中英文等多语言环境
- angular.js 国际化模块 angular-translate 简单方便快捷翻译中英文等多语言环境
- ubuntu中英文语言环境的设置
- Android-国际化(多语言)切换详解及实例
- Java Web的Struts2的多语种网站的多语言切换实例
- 基于手机语言环境进行语言匹配(适配中英文之外的所有语言均为英文)
- linux环境中英文切换配置以及乱码问题