Posted on 2009-01-20 14:47
非洲小白臉 閱讀(170)
評論(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
就可以正常執行了。