JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Create simple custom Converter implementation class in JSF

July 8, 2008 by Krishna Srinivasan Leave a Comment

Custom Converter Class Implementation

This article explains the simple Converter class implementation. Converter class is used for converting any given input to the desired output format or with any business logic to the input values.

also read:

  • Introduction to JSF
  • JSF Interview Questions
  • Request Processing Lifecycle phases in JSF

This example also includes the PhaseListener to identify in which JSF lifecycle phase the convertion happens.

JSP File (index.jsp)

[code lang=”html”]
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
<body>
<f:view>
<h:form>
<h:inputText value="#{jsfBean.field}">
<f:converter converterId="customConverter"/>
</h:inputText>
<h:commandButton action="#{jsfBean.submit}" value="Submit">
<f:phaseListener binding="#{jsfBean.phaseListenerImpl}" type="javabeat.jsf.PhaseListenerImpl"/>
</h:commandButton>
</h:form>
</f:view>
</body>
</html>[/code]

JavaBean (JavaBeatJsfBean.java)

[code lang=”java”]
package javabeat.jsf;

import javax.faces.event.ActionEvent;

/**
* source : www.javabeat.net
*/
public class JavaBeatJsfBean {

private String field;
private CustomConverter customConverter;
private PhaseListenerImpl phaseListenerImpl;

public PhaseListenerImpl getPhaseListenerImpl() {
return phaseListenerImpl;
}

public void setPhaseListenerImpl(PhaseListenerImpl phaseListenerImpl) {
this.phaseListenerImpl = phaseListenerImpl;
}

public CustomConverter getCustomConverter() {
return customConverter;
}

public void setCustomConverter(CustomConverter customConverter) {
this.customConverter = customConverter;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

public String submit() {
System.out.println(this.field);
return "success";
}
}[/code]

CustomConverter.java

[code lang=”java”]
package javabeat.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
/**
* source : www.javabeat.net
*/
public class CustomConverter implements Converter{

/**
* This method used for getting the input and process and convert.
*/
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
System.out.println("Input from the user : "+value);
System.out.println("Use custom convert logic and process the input");
return "converted Value";
}

/**
* This method used for return the converted value to the user.
*/
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
System.out.println("Value to be displayed to the user"+value);
String output = (String)value;
return output;
}
}

[/code]

PhaseListenerImpl.java

[code lang=”java”]
package javabeat.jsf;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
/**
* source : www.javabeat.net
*/
public class PhaseListenerImpl implements PhaseListener{
public void afterPhase(PhaseEvent event) {
System.out.println("After Executing " + event.getPhaseId());
}
public void beforePhase(PhaseEvent event) {
System.out.println("Before Executing " + event.getPhaseId());
}
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}

[/code]

faces-config.xml

[code lang=”xml”]
<?xml version=’1.0′ encoding=’UTF-8′?>
<faces-config version="1.2"
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-facesconfig_1_2.xsd">
<managed-bean>
<managed-bean-name>jsfBean</managed-bean-name>
<managed-bean-class>javabeat.jsf.JavaBeatJsfBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<converter>
<description>A Converter</description>
<converter-id>customConverter</converter-id>
<converter-class>
javabeat.jsf.CustomConverter
</converter-class>
</converter>
</faces-config>

[/code]

Output in the command prompt

[code]
Before Executing RENDER_RESPONSE 6
After Executing RENDER_RESPONSE 6
Before Executing APPLY_REQUEST_VALUES 2
After Executing APPLY_REQUEST_VALUES 2
Before Executing PROCESS_VALIDATIONS 3
Input from the user : input Value
Use custom convert logic and process the input
After Executing PROCESS_VALIDATIONS 3
Before Executing UPDATE_MODEL_VALUES 4
After Executing UPDATE_MODEL_VALUES 4
Before Executing INVOKE_APPLICATION 5
converted Value
After Executing INVOKE_APPLICATION 5
Before Executing RENDER_RESPONSE 6
Value to be displayed to the userconverted Value
After Executing RENDER_RESPONSE 6[/code]

Filed Under: JSF Tagged With: Converter

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved