W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
我們可以在JSF中創(chuàng)建我們自己的自定義轉(zhuǎn)換器。
以下列表是我們可以按照在JSF中創(chuàng)建自定義轉(zhuǎn)換器的步驟。
以下代碼來自URLConverter.java。
package cn.w3cschool.common; 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("cn.w3cschool.common.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() > 10){ 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(); } }
下面的代碼來自UserBean.java。
package cn.w3cschool.common; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ String bookmarkURL; public String getBookmarkURL() { return bookmarkURL; } public void setBookmarkURL(String bookmarkURL) { this.bookmarkURL = bookmarkURL; } }
以下代碼來自result.xhtml。
<?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>
以下代碼來自demo.xhtml。
<?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="20" required="true" label="Bookmark URL"> <f:converter converterId="cn.w3cschool.common.URLConverter" /> </h:inputText> <h:message for="bookmarkURL" style="color:red" /> </h:panelGrid> <h:commandButton value="Submit" action="result" /> </h:form> </h:body> </html>下載 Custom-Converter.zip
將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動(dòng)后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: