JSF Custom Converter Example:
Let us discuss how to create custom converter in JSF with example.
Steps to create custom converter in JSF:
1. First create a converter class by implementing javax.faces.convert.Converter interface
2. Override getAsObject() and getAsString() methods of above interface.
3. Assign a unique converter ID to the custom convertor by using @FacesConverter annotation.
4. Use custom converter class to JSF component via f:converter tag.
Example:
test.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:ui="http://java.sun.com/jsf/facelets"> <h:head> <title>JSF custom converter example.</title> </h:head> <h:body> <h3>JSF custom converter example.</h3> <h:form> <h:inputText id="urlId" value="#{test.urlDetail}" label="Enter URL: " > <f:converter converterId= "com.w3schools.business.URLConverter" /> </h:inputText> <br/><br/> <h:commandButton value="Submit" action="welcome"/> <h:message for="urlId" style="color:red" /> </h:form> </h:body> </html> |
Test.java
import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; /** * Managed bean. * @author w3schools */ @ManagedBean(name="test") @SessionScoped public class Test { private URLDetail urlDetail; public URLDetail getUrlDetail() { return urlDetail; } public void setUrlDetail(URLDetail urlDetail) { this.urlDetail = urlDetail; } } |
URLConverter.java
import java.net.URI; import java.net.URISyntaxException; 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.w3schools.business.URLConverter") public class URLConverter implements Converter { @Override public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { StringBuilder url = new StringBuilder(); if (!value.startsWith("http://", 0)) { url.append("http://"); } url.append(value); try { new URI(url.toString()); } catch (URISyntaxException e) { FacesMessage msg = new FacesMessage("Error converting URL", "Invalid URL format"); msg.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(msg); } URLDetail urlData = new URLDetail(url.toString()); return urlData; } @Override public String getAsString(FacesContext facesContext, UIComponent component, Object value) { return value.toString(); } } |
URLDetail.java
public class URLDetail { private String url; public URLDetail(String url){ this.url = url; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String toString(){ return url; } } |
welcome.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:ui="http://java.sun.com/jsf/facelets"> <h:head> <title>JSF custom converter example.</title> </h:head> <h:body> <h3>JSF custom converter example.</h3> <h:outputText label="URL: " value="#{test.urlDetail.url}" /> </h:body> </html> |
faces-config.xml
<?xml version="1.0" encoding="windows-1252"?> <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> </faces-config> |
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>faces</servlet-name> <servlet-class> javax.faces.webapp.FacesServlet <servlet-mapping> <servlet-name>faces</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> </web-app> |
URL:
http://localhost:7001/JSFExample31/faces/test.xhtml
Output:
Enter any website name.
Click Submit button.
Download this example.