鍍金池/ 教程/ HTML/ JSF自定義轉(zhuǎn)換器
JSF數(shù)據(jù)表(h:dataTable)添加刪除
JSF <h:commandLink>標簽
JSF應(yīng)用程序入門示例
JSF數(shù)據(jù)表(ui:repeat)創(chuàng)建表
JSF列表框
JSF數(shù)據(jù)表(h:dataTable)DataModel排序數(shù)據(jù)
JSF復(fù)合組件
JSF <h:inputText>標簽
JSF表單組合框
JSF <h:messages>標簽
JSF <h:message>標簽
JSF轉(zhuǎn)換日期時間
JSF JDBC連接
JSF <h:inputHidden>標簽
JSF多選列表框
JSF <h:inputSecret>標簽
JSF自定義轉(zhuǎn)換器
JSF <f:ajax>標簽
JSF生命周期
JSF可重定位資源
JSFJSF用戶界面組件模型
JSF <h:attribute>標簽
JSF驗證器標簽
JSF驗證字符串長度
JSF轉(zhuǎn)換器標簽
JSF托管bean(Managed Bean)
JSF值變化的事件
JSF UI組件示例
JSF MySQL CURD實例
JSF數(shù)據(jù)表(h:dataTable)排序數(shù)據(jù)
JSF <h:graphicImage>標簽
JSF <f:convertNumber>標簽
JSF教程
JSF托管Bean
JSF輸出腳本
JSF <h:outputText>標簽
JSF操作事件
JSF驗證正則表達式
JSF數(shù)據(jù)表(h:dataTable)行號
JSF <h:setPropertyActionListener>標簽
JSF注入托管bean實例
JSF <h:commandButton>標簽
JSF Web資源
JSF <h:inputFile>標簽
JSF驗證浮點數(shù)值范圍
JSF Facelets視圖
JSF是什么?
JSF Facelets模板
JSF的特性(特點)
JSF自定義驗證器類
JSF單選按鈕
JSF輸出樣式
JSF數(shù)據(jù)表(h:dataTable)更新數(shù)據(jù)
JSF HTML5友好標記
JSF表單復(fù)選框(CheckBox)示例
JSF <h:form>標簽
JSF Facelets技術(shù)介紹
JSF輸出格式化
JSF <h:inputtextarea>標簽
JSF驗證整數(shù)范圍
JSF <h:panelGrid>標簽

JSF自定義轉(zhuǎn)換器

我們可以在JSF中創(chuàng)建自己的自定義轉(zhuǎn)換器。
以下列表是我們可以在JSF中創(chuàng)建自定義轉(zhuǎn)換器的步驟。

  1. 通過實現(xiàn)javax.faces.convert.Converter接口創(chuàng)建一個轉(zhuǎn)換器類。
  2. 實現(xiàn)上述接口的getAsObject()getAsString()方法。
  3. 使用注解@FacesConvertor為自定義轉(zhuǎn)換器分配唯一的ID。

JSF自定義轉(zhuǎn)換器實例

打開 NetBeans IDE 創(chuàng)建一個Web工程:CustomConverter,其目錄結(jié)構(gòu)如下所示 -

創(chuàng)建以下文件代碼,文件:index.xhtml 的代碼內(nèi)容如下所示 -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
        <h:form>
            <h:panelGrid columns="3">
                Enter your bookmark URL :
                <h:inputText id="bookmarkURL" value="#{user.bookmarkURL}" 
                             size="40" required="true" label="Bookmark URL">
                    <f:converter converterId="com.yiibai.URLConverter" />
                </h:inputText>

                <h:message for="bookmarkURL" style="color:red" />

            </h:panelGrid>

            <h:commandButton value="Submit" action="result" />

        </h:form>

    </h:body>
</html>

文件:result.xhtml 的代碼內(nèi)容如下所示 -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      >
    <h:body>
        <h:panelGrid columns="2">

            Bookmark URL :  
            <h:outputText value="#{user.bookmarkURL}" />

        </h:panelGrid>

    </h:body>
</html>

文件:User.java 的代碼內(nèi)容如下所示 -

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yiibai;

/**
 *
 * @author Administrator
 */
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;
import javax.faces.convert.FacesConverter;

@FacesConverter("com.yiibai.URLConverter")
public class URLConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String value) {

        String HTTP = "http://";
        StringBuilder url = new StringBuilder();

        if (!value.startsWith(HTTP, 0)) {
            url.append(HTTP);
        }
        url.append(value);

        if (url.toString().length() > 30) {
            FacesMessage msg
                    = new FacesMessage("URL Conversion error.",
                            "Invalid URL format.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ConverterException(msg);
        }
        return url.toString();
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {

        return value.toString();
    }
}

右鍵運行工程:CustomConverter,如果沒有任何錯誤,打開瀏覽器訪問:

http://localhost:8084/CustomConverter/

應(yīng)該會看到以下結(jié)果 -

簡單寫入一些信息,然后提交 -