1.JSF轉換器的應用
Web應用程式與瀏覽器之間傳送的資料基本上都是字串文字,而Java應用程式本身基本上則是物件,所以物件資料必須經由轉換傳送給瀏覽器.
JSF定義了一系列標準的轉換器(Converter),對于基本資料型態
(primitive type)或是其Wrapper類別,
JSF會使用
javax.faces.Boolean、
javax.faces.Byte、
javax.faces.Character、
javax.faces.Double、
javax.faces.Float、
javax.faces.Integer、
javax.faces.Long、
javax.faces.Short等自動進行轉換,對于BigDecimal、BigInteger,則會使用javax.faces.BigDecimal、javax.faces.BigInteger自動進行轉換。
DateTime、Number,我們可以使用<f:convertDateTime>、<f:convertNumber>標簽進行轉換。
當我們需要在頁面上綁定或者顯示Bean里的信息的時候,如果Bean里的字段不是String類型的,則需要將其先轉換成String類型。
例:
先定義一個Bean,里面包含了一個Date類型的變量。
package test;
import java.util.Date;
//JSF的轉換器示例
public class JsfCoverter {
private Date date = new Date();
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
再建立一個Jsp,用來顯示JsfCoverter里面的變量date。
<%@ 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>
<html>
<head>
<title>轉換器示范</title>
</head>
<body>
<b>
<h:outputText value="#{jsfcover.date}">
<f:convertDateTime pattern="dd/MM/yyyy"
/>
</h:outputText> </b>
<h:form>
<h:inputText
id="dateField" value="#{jsfcover.date}">
<f:convertDateTime
pattern="dd/MM/yyyy" />
</h:inputText>
<h:message for="dateField"
style="color:red" />
<br>
<h:commandButton value="送出"
action="show" />
</h:form>
</body>
</html>
</f:view>
在<f:convertDateTime>中指定了pattern的樣式為dd/MM/yyyy,你在<h:inputText>中輸入的是String類型的信息,但是和這個字段綁定的是一個Date變量(jsfcover.date),
因此你必須將輸入的String類型轉換成Date類型才行。而<f:convertDateTime>就是幫我們做這件事的。
如果轉換出錯的話,<h:message>則會顯示錯誤信息。
當然,我們還需要配置定義的Bean,以及設子頁面的跳轉,才能正常運行。
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>show</from-outcome>
<to-view-id>/pages/jsfCoverter.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<!--
jsf轉換器 -->
<managed-bean>
<managed-bean-name>jsfcover</managed-bean-name>
<managed-bean-class>test.JsfCoverter</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
啟動Tomcat,就可以運行了http://localhost:8080/jsfTest/pages/jsfCoverter.faces
具體程序參看示例程序.
2.自定義JSF轉換器
除了使用標準的轉換器之外,您還可以自行定制您的轉換器,您可以實作javax.faces.convert.Converter接口,這個接口有兩個要實作的方法:
public
Object getAsObject(FacesContext context,
UIComponent component,
String
str);
public String
getAsString(FacesContext context,
UIComponent component,
Object
obj);
簡單的說,第一個方法會接收從客戶端經由HTTP傳來的字符串數據,您在第一個方法中將之轉換為您的自訂對象,這個自訂對象將會自動設定給您指定的Bean對象;
第二個方法就是將從您的Bean對象得到的對象轉換為字符串,如此才能藉由HTTP傳回給客戶端。
上面的<f:convertDateTime>轉換器可以將用戶輸入的String類型的信息轉換成Bean的date變量,顯示時再由Date型轉成String顯示在頁面上。
但是很多項目需要將用戶輸入的或者檢索的不規則的Date格式統一化,這就需要用戶自己定義一個共通的轉換器了。
首先建立一個轉換器,它要實現javax.faces.convert.Converter接口:
package
test;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
javax.faces.application.FacesMessage;
import
javax.faces.component.UIComponent;
import
javax.faces.context.FacesContext;
import
javax.faces.convert.Converter;
import
javax.faces.convert.ConverterException;
public class JsfMyCoverter implements Converter {
public Object getAsObject(FacesContext
context, UIComponent component,
String obj) {
// TODO Auto-generated method stub
try {
Date date = new Date();
String objFor =
obj.substring(0,4) + "-"
+ obj.substring(4,6) + "-"
+
obj.substring(6,8);
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(objFor);
return date;
} catch (Exception e) {
FacesMessage
facesMessage = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"日期格式錯誤", "日期格式錯誤");
throw new
ConverterException(facesMessage);
}
}
public String getAsString(FacesContext
context, UIComponent component,
Object obj) {
// TODO Auto-generated method stub
String res = null;
if (obj instanceof Date) {
String pattern =
"yyyy-MM-dd";
SimpleDateFormat sdf = new
SimpleDateFormat(pattern);
res = sdf.format(obj);
return res;
}
return res;
}
}
同時定義表示頁面jsfMyCoverter.jsp.
別忘記在faces-config.xml中完成注冊:
<!--
自定義轉換器 -->
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>myConverter</from-outcome>
<to-view-id>/pages/jsfMyCoverter.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<!--
自定義轉換器 -->
<converter>
<converter-id>dateFormatCoverter</converter-id>
<converter-class>
test.jsfMyCoverter
</converter-class>
</converter>
更新Tomcat,http://localhost:8080/jsfTest/pages/jsfMyCoverter.faces就可以看到自定義的轉換器了。
具體程序參看示例程序。