〖 作者:qjyong 〗〖 大小:1k 〗〖 發(fā)布日期:2007-12-16 〗〖 瀏覽:0 〗
Source: http://www.java3z.com/cwbwebhome/article/article2/21023.html?id=1735
問題描述:實(shí)現(xiàn)一個帶自選語言欄的用戶登錄驗(yàn)證示例的國際化。對于這個實(shí)例分兩部分來實(shí)現(xiàn):先實(shí)現(xiàn)用戶登錄驗(yàn)證的國際化,再加上自選語言欄。
第一部分:實(shí)現(xiàn)用戶登錄驗(yàn)證
創(chuàng)建一個名為I18N_demo的JSF Web項(xiàng)目。
1. 創(chuàng)建后臺Bean
在項(xiàng)目中創(chuàng)建一個后臺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(),在這個方法中需要注意的是,對用戶名、密碼都進(jìn)行了相應(yīng)的判斷,如果是test、123456,就是合法用戶,返回結(jié)果字符串“success”,否則是非法用戶,通過JSF提供的MessageFactory來獲取并創(chuàng)建好一則本地化錯誤消息(消息“鍵”是“field_ISERROR”),添加到FacesContext中,然后返回結(jié)果字符串“failure”。這樣到了失敗頁面就可以取出相應(yīng)的經(jīng)過本地化的錯誤消息。
2. 配置托管Bean和資源文件綁定
在faces-config.xml文件中把RegistrationBean配置成托管Bean。同時為了支持國際化,指定了錯誤消息文件和資源文件,它們是同一個文件,就是存放在應(yīng)用的org/qiujy/web/resources目錄下的ApplicationMessages.properties文件,稍后再來看這個文件的內(nèi)容:
<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. 創(chuàng)建頁面和本地化資源文件
用戶登錄頁面: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>
在這個頁面中,所有靜態(tài)文本,錯誤消息都通過值表達(dá)式用資源文件的別名“bundle”來獲取的。所有的資源消息“鍵”在本地化資源文件中都配置了相應(yīng)的“值”,如下:
代碼片段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的內(nèi)容跟這個相同。下面再來看簡體中文的資源文件:
簡體中文的資源文件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} 不正確
需要注意是,使用是別忘了進(jìn)行Uncodei編碼轉(zhuǎn)換。至于繁體中文的資源文件也跟這個文件差不多,在此不再贅述。
另外要對標(biāo)準(zhǔn)的錯誤消息進(jìn)行國際化,可以把SUN的RI實(shí)現(xiàn)中的錯誤消息全部復(fù)制到本地化資源文件中,對簡體中文的資源進(jìn)行漢化,由于內(nèi)容較多,在這就不帖出代碼了,具體可能看本例的源代碼。
接下來看登錄成功后的頁面的代碼: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>
在這個頁面中,為了演示日期時間的國際化,先創(chuàng)建了一個日期對象,然后用Output組件標(biāo)簽輸出,并給這個標(biāo)簽注冊了DateTimeConverter,這樣就能實(shí)現(xiàn)日期時間的國際化了。
最后再來看登錄失敗頁面的代碼: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/>
<h:outputLink value="userlogin.faces">
<h:outputText value="#{bundle.button_back}"/>
</h:outputLink>
</body>
</html>
</f:view>
在這個頁面,用<h:message>標(biāo)簽輸出了登錄失敗的原因,這個錯誤消息是在后臺Bean中的validate()方法中從資源文件中引用的經(jīng)過本地化后的消息。
4. 配置導(dǎo)航規(guī)則
導(dǎo)航規(guī)則比較簡單,直接看代碼:
<navigation-rule>
<from-view-id>/userlogin.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
第二部分: 實(shí)現(xiàn)自選語言欄
實(shí)現(xiàn)頁面的語言可以讓用戶根據(jù)自己的喜好進(jìn)行選擇,也是在做web應(yīng)用時比較常見的一個功能。下面就來完成這個任務(wù)。
1. 創(chuàng)建后臺Bean
在這個應(yīng)用中添加一個處理用戶自選語言的后臺Bean,LanguageSelectorBean,代碼如下:
package org.qiujy.common;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
public class LanguageSelectorBean {
private String currentLocale;
// 本應(yīng)用支持的語言Map
private Map<String, Locale> supportLocals;
public LanguageSelectorBean() {
supportLocals = new HashMap<String, Locale>();
supportLocals.put("zh_CN", Locale.CHINA);
supportLocals.put("zh_TW", Locale.TAIWAN);
supportLocals.put("en", Locale.ENGLISH);
this.currentLocale = FacesContext.getCurrentInstance().getViewRoot()
.getLocale().toString();
}
public String getCurrentLocale() {
return currentLocale;
}
public void setCurrentLocale(String currentLocale) {
this.currentLocale = currentLocale;
}
// 改變當(dāng)前語言區(qū)域的事件處理方法
public void changeLocale(ValueChangeEvent event) {
String currentLocale = (String) event.getNewValue();
// 設(shè)置當(dāng)前的語言區(qū)域
Locale myLocale = this.supportLocals.get(currentLocale);
FacesContext.getCurrentInstance().getViewRoot().setLocale(myLocale);
// 把自定義語言存放到Session中
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
request.getSession().setAttribute("myLocale", myLocale);
}
}
在這個后臺Bean中有一個值改變事件處理方法changeLocale(),它根據(jù)用戶選中的語言代碼來更改當(dāng)前FacesContext中的視圖的默認(rèn)語言區(qū)域。同時,為了讓當(dāng)前用戶的整個會話過程的語言區(qū)域都是用戶的自行選擇的,需要把用戶選擇的語言區(qū)域?qū)ο蟠娣诺絊ession中。
2. 創(chuàng)建階段事件監(jiān)聽器來設(shè)置自選語言環(huán)境
用戶設(shè)置好語言環(huán)境后,要想在當(dāng)前會話的后續(xù)請求中繼續(xù)生效,就要在所有JSF請求處理生命周期的“呈現(xiàn)響應(yīng)階段”發(fā)生前更改當(dāng)前FacesContext中的視圖的默認(rèn)語言區(qū)域,這個任務(wù)交由階段事件監(jiān)聽器來實(shí)現(xiàn)。所在,在本應(yīng)用中還要添加一個階段事件監(jiān)聽器的實(shí)現(xiàn)類。如:
package org.qiujy.common;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;
/**
* 階段事件監(jiān)聽器實(shí)現(xiàn)類:主要是用來設(shè)置自選語言環(huán)境
*/
@SuppressWarnings("serial")
public class MyPhaseListener implements PhaseListener {
/**
* 結(jié)束某個階段時會調(diào)用到的方法
*/
public void afterPhase(PhaseEvent pe) {
}
/**
* 開始某個階段時會調(diào)用到的方法
*/
public void beforePhase(PhaseEvent pe) {
System.out.println("this is Phase: " + pe.getPhaseId().toString());
// 呈現(xiàn)響應(yīng)階段,設(shè)置自選語言環(huán)境
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
Locale myLocale = (Locale) request.getSession().getAttribute("myLocale");
System.out.println("current request locale is: "
+ FacesContext.getCurrentInstance().getViewRoot().getLocale()
.toString());
if (myLocale != null) {
FacesContext.getCurrentInstance().getViewRoot().setLocale(myLocale);
System.out.println("current Custom locale is: "
+ FacesContext.getCurrentInstance().getViewRoot()
.getLocale().toString());
}
}
/**
* 只監(jiān)聽"呈現(xiàn)響應(yīng)階段"
*/
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}
這個類實(shí)現(xiàn)了PhaseListener接口,通過在getPhashId()方法中返回PhaseId.RENDER_RESPONSE來監(jiān)聽“呈現(xiàn)響應(yīng)階段”事件,在開始“呈現(xiàn)響應(yīng)階段”事件時,會調(diào)用本類的beforePhase()方法,在這個方法中,通過從Session對象中取出用戶設(shè)置好的語言環(huán)境,來更改當(dāng)前FacesContext中的視圖的默認(rèn)語言區(qū)域。這樣,當(dāng)前會話的后續(xù)響應(yīng)都會用用戶選擇的語言環(huán)境來呈現(xiàn)視圖了。注意,要把此監(jiān)聽器類配置在faces-config.xml中,它的作用才生效,如:
<lifecycle>
<phase-listener>
org.qiujy.common.MyPhaseListener
</phase-listener>
</lifecycle>
3. 配置托管Bean和資源文件綁定
像其它托管Bean一樣,把它配置在faces-config.xml文件中,但要注意一點(diǎn)的是,這個托管Bean的存放范圍應(yīng)該選擇session,這樣才能保證對語言區(qū)域的選擇在當(dāng)前用戶的整個會話過程中都有效。
<managed-bean>
<managed-bean-name>languageSelectorBean</managed-bean-name>
<managed-bean-class>
org.qiujy.common.LanguageSelectorBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
4. 創(chuàng)建頁面和本地化資源文件
接下來創(chuàng)建一個語言選擇欄頁面:
<%@ 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:subview id="languageView">
<h:form id="languageForm"
style="background-color:#bbbbbb; padding-top:4px; padding-bottom:4px;">
<h:outputText value="#{bundle.selectYourLocale}"></h:outputText>
<h:selectOneMenu value="#{languageSelectorBean.currentLocale}"
immediate="true" onchange="submit();"
valueChangeListener="#{languageSelectorBean.changeLocale}">
<f:selectItem itemValue="zh_CN" itemLabel="#{bundle.zh_CNText}" />
<f:selectItem itemValue="zh_TW" itemLabel="#{bundle.zh_TWText}" />
<f:selectItem itemValue="en" itemLabel="#{bundle.enText}" />
</h:selectOneMenu>
</h:form>
</f:subview>
這個頁面的特殊之處有三處:
q 它的根視圖標(biāo)簽是<f:subview>,而不是常用的<f:view>了,這是因?yàn)檫@個頁面經(jīng)常是用來內(nèi)嵌到其它頁面的,而JSF規(guī)范又要求一個頁面只能有一個<f:view>頂層視圖,所以就只能用<f:subview>來創(chuàng)建一個子視圖。
q 給selectOneMenu組件添加了一個值改變事件監(jiān)聽器,同時添加了一個JavaScript的onchange事件來提交表單,這樣在改變下拉菜單的選中項(xiàng)時,會提交表單,同時會觸發(fā)值改變事件。這樣就能實(shí)現(xiàn)改變語言區(qū)域的功能了。
q 把selectOneMenu組件的immediate屬性值設(shè)置為true,這樣可以使這個組件提交表單時,請求處理生命周期的應(yīng)用請求階段就會調(diào)用值改變事件的處理方法,從而把應(yīng)用驗(yàn)證“短路”掉,這樣即使頁面的一些必填字段(本例中的用戶名,密碼)沒有輸入值,也不會報錯了。
最后,還要在所有的本地化資源文件中分別加上這個頁面中出現(xiàn)的消息“鍵-值”對,例如在缺省的資源文件中添加如下代碼:
zh_CNText=Chinese,Simplify
zh_TWText=Chinese,Traditional
enText=English
selectYourLocale=Select Language:
5. 在頁面上包含自選語言欄頁面
在用戶登錄頁面中用JSP的include動作把自選語言欄頁面包含進(jìn)來,在userlogin.jsp頁面body體的首行添加如下代碼:
<jsp:include page="languageSelector.jsp"/>
6. 運(yùn)行查看效果
到此整個應(yīng)用開發(fā)完畢,項(xiàng)目目錄結(jié)構(gòu)如圖:
自選語言項(xiàng)目目錄結(jié)構(gòu)圖
重新部署應(yīng)用,運(yùn)行,訪問主頁:
中文主頁圖
通過在下拉列表選中“英文”來更新想要的英文界面:
英文主頁圖