爪哇一角

共同探讨STRUTS#HIBERNATE#SPRING#EJB等技术
posts - 3, comments - 6, trackbacks - 0, articles - 99
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

三 JSF国际编码支持

Posted on 2009-01-20 14:47 非洲小白脸 阅读(156) 评论(0)  编辑  收藏 所属分类: JSF

JSF国际化(Internnationalization)讯息处理是基于Java对国际化的支援,您可以在一个讯息资源档中统一管理讯息资源,messages_en.properties

例如:

nameText=name
passText=password

 

同时,你也可以定义对应于中文的统一管理讯息资源,messages_zh_TW.properties

例如:

nameText=\u540d\u7a31

passText=\u5bc6\u78bc

资源中的文字需要进行Unicode重新编码,可以用jdk自带的native2ascii工具实现。

JsfInternMessage.jsp

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@page contentType="text/html;charset=utf-8"%>

 

<f:view locale="#{user.locale}">

       <f:loadBundle basename="messages" var="msgs" />

       <html>

       <head>

       <title><h:outputText value="#{msgs.titleText}" /></title>

       </head>

       <body>

       <h:form>

              <!—请输入你的姓名 -->

              <h3><h:outputText value="#{msgs.hintText}" /></h3>

              <!-- 姓名 -->

              <h:outputText value="#{msgs.nameText}" />:

<h:inputText value="#{user.name}" />

              <p><!-- 送出 --><h:commandButton id="button1"

                     value="#{msgs.commandText}" action="success" /> <!-- 调用不同的资源文件,改变页面的表示语言 -->

              <h:commandButton value="#{msgs.Text}"

                     actionListener="#{user.changeLocale}" />

触发user里的方法,返回不同的值,从而达到调用不同资源文件的目的。

 

 
       </h:form>

       </body>

       </html>

</f:view>

 

UserBean.java

public void changeLocale(ActionEvent e) {

        if(locale.equals("en"))

        locale = "zh_TW";

        else

        locale = "en";

    }

具体代码参看示例程序.(JsfInternMessage.jsp,UserBean.java,messages_en.properties,messages_zh_TW.properties)

 

注意:JSP页面头部要用下面的标签包住。

<f:view locale="#{user.locale}">

<f:loadBundle basename="messages" var="msgs"/>

上面标签的意思是,本JSP调用messages_"#{user.locale}".properties资源文件,如果user.locale=en,则调用messages_en.properties文件。

http://localhost:8080/jsfTest/pages/jsfInternMessage.faces 就可以正常执行了。

 


只有注册用户登录后才能发表评论。


网站导航: