We can create our own Custom convertor in JSF.
The following list is the step we can follow to create a custom converter in JSF.
The following code is from URLConverter.java.
package com.java2s.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("com.java2s.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(); } }
The following code is from UserBean.java.
package com.java2s.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; } }
The following code is from 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>
The following code is from 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="com.java2s.common.URLConverter" /> </h:inputText> <h:message for="bookmarkURL" style="color:red" /> </h:panelGrid> <h:commandButton value="Submit" action="result" /> </h:form> </h:body> </html>
Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.
After Tomcat finish starting, type the following URL in the browser address bar.
http://localhost:8080/simple-webapp/demo.xhtml