作者:++yong
奉送我的JSF書稿中的一個實例:JSF實現的自選語言界面
版權聲明:本文可以自由轉載,轉載時請務必標明作者信息及本聲明
作者:++yong
作者:++yong
作者的Blog:http://blog.csdn.net/qjyong
問題描述:實現一個帶自選語言欄的用戶登錄驗證示例的國際化。對於這個實例分兩部分來實現:先實現用戶登錄驗證的國際化,再加上自選語言欄。
第一部分:實現用戶登錄驗證
創建一個名爲I18N_demo的JSF Web項目。
1. 創建後臺Bean
在項目中創建一個後臺Bean:RegistrationBean.java
package org.qiujy.web.controller;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class RegistrationBean {
private String userName;
private String password;
//以下是屬性的getter和setter方法
......
public String validate() {
boolean flag = true;
if (!"test".equals(userName)) {
FacesMessage msg = MessageFactory.getMessage(FacesContext
.getCurrentInstance(), "field_ISERROR",
new Object[] { "userName" });
FacesContext.getCurrentInstance().addMessage(null, msg);
flag = false;
}
if (!"123456".equals(password)) {
FacesMessage msg = MessageFactory.getMessage(FacesContext
.getCurrentInstance(), "field_ISERROR",
new Object[] { "password" });
FacesContext.getCurrentInstance().addMessage(null, msg);
flag = false;
}
if (flag) {
return "success";
} else {
return "failure";
}
}
}
這個Bean中提供跟頁面綁定的屬性,以及跟動作按鈕綁定的動作處理方法validate(),在這個方法中需要注意的是,對用戶名、密碼都進行了相應的判斷,如果是test、123456,就是合法用戶,返回結果字符串“success”,否則是非法用戶,通過JSF提供的MessageFactory來獲取並創建好一則本地化錯誤消息(消息“鍵”是“field_ISERROR”),添加到FacesContext中,然後返回結果字符串“failure”。這樣到了失敗頁面就可以取出相應的經過本地化的錯誤消息。
2. 配置託管Bean和資源文件綁定
在faces-config.xml文件中把RegistrationBean配置成託管Bean。同時爲了支持國際化,指定了錯誤消息文件和資源文件,它們是同一個文件,就是存放在應用的org/qiujy/web/resources目錄下的ApplicationMessages.properties文件,稍後再來看這個文件的內容:
<faces-config>
<application>
<message-bundle>
org.qiujy.web.resources.ApplicationMessages
</message-bundle>
<locale-config>
<default-locale>zh_CN</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>zh_TW</supported-locale>
</locale-config>
<resource-bundle>
<base-name>
org.qiujy.web.resources.ApplicationMessages
</base-name>
<var>bundle</var>
</resource-bundle>
</application>
<managed-bean>
<managed-bean-name>registrationBean</managed-bean-name>
<managed-bean-class>
org.qiujy.web.controller.RegistrationBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
......
</faces-config>
3. 創建頁面和本地化資源文件
用戶登錄頁面:userlogin.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<html>
<head>
<title><h:outputText value="#{bundle.title_login}" /></title>
</head>
<body>
<h:form id="loginForm">
<h:panelGrid columns="2">
<h:graphicImage url="#{bundle.login_logo}"
width="220" height="160"/>
<h:panelGrid columns="3">
<f:facet name="caption">
<h:outputText value="#{bundle.title_login}" />
</f:facet>
<h:outputText value="#{bundle.login_userName}" />
<h:inputText id="textName"
value="#{registrationBean.userName}"
required="true">
</h:inputText>
<h:message for="textName" style="color:red" />
<h:outputText value="#{bundle.login_password}" />
<h:inputSecret id="textPwd"
value="#{registrationBean.password}"
required="true">
<f:validateLength minimum="6" maximum="20"/>
</h:inputSecret>
<h:message for="textPwd" style="color:red" />
<f:facet name="footer">
<h:panelGroup>
<h:commandButton value="#{bundle.button_submit}"
action="#{registrationBean.validate}" />
<h:outputText value=" "></h:outputText>
<h:commandButton value="#{bundle.button_reset}"
type="reset" />
</h:panelGroup>
</f:facet>
</h:panelGrid>
</h:panelGrid>
</h:form>
</body>
</html>
</f:view>
在這個頁面中,所有靜態文本,錯誤消息都通過值表達式用資源文件的別名“bundle”來獲取的。所有的資源消息“鍵”在本地化資源文件中都配置了相應的“值”,如下:
代碼片段7.15 缺省的資源文件ApplicationMessages.properties
button_submit=Submit
button_reset=Reset
button_back=Back
title_login=User Login Page
login_userName=UserName:
login_password=Password:
login_logo=/images/jsf_i18n_en.gif
success_welcome=Welcome:
failure_error=Failure!
field_ISERROR= {0} is error.
英文的資源文件ApplicationMessages_en.properties的內容跟這個相同。下面再來看簡體中文的資源文件:
簡體中文的資源文件ApplicationMessages_zh_CN.properties
button_submit=提交
button_reset=重置
button_back=後退
title_login=用戶登錄頁面
login_userName=用戶名:
login_password=密碼::
login_logo=/images/jsf_i18n_zh_CN.gif
success_welcome=歡迎:
failure_error=失敗!
field_ISERROR= {0} 不正確
需要注意是,使用是別忘了進行Uncodei編碼轉換。至於繁體中文的資源文件也跟這個文件差不多,在此不再贅述。
另外要對標準的錯誤消息進行國際化,可以把SUN的RI實現中的錯誤消息全部複製到本地化資源文件中,對簡體中文的資源進行漢化,由於內容較多,在這就不帖出代碼了,具體可能看本例的源代碼。
接下來看登錄成功後的頁面的代碼:success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<html>
<head>
<title><h:outputText value="#{bundle.success_welcome}"/></title>
</head>
<body>
<h2>
<h:outputText value="#{bundle.success_welcome}" />
<h:outputText value="#{registrationBean.userName}" />
</h2>
<jsp:useBean id="currentDate" class="java.util.Date" scope="request"/>
<h:outputText value="#{currentDate}">
<f:convertDateTime type="both"/>
</h:outputText>
</body>
</html>
</f:view>
在這個頁面中,爲了演示日期時間的國際化,先創建了一個日期對象,然後用Output組件標籤輸出,並給這個標籤註冊了DateTimeConverter,這樣就能實現日期時間的國際化了。
最後再來看登錄失敗頁面的代碼:failure.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html>
<head>
<title><h:outputText value="#{bundle.failure_error}"/></title>
</head>
<body>
<h2><h:outputText value="#{bundle.failure_error}"/></h2>
<h:messages style="color:red"/><br/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.