Seam: Creating a custom converter

///Seam: Creating a custom converter

Seam: Creating a custom converter

I was developing an application with Seam Framework. At a certain scenario for some reason I was needed to use [highlight type=”special_text”]h:inputText[/highlight] to bind a bean. Later on I found that it was raising an validation error message saying, [highlight type=”special_text”]Conversion Error setting value ‘xxx’ for ‘null Converter'[/highlight].

[xml]<h:inputText value="#{lcInfo.itemIndentMastBean}" id="indentBean" required="true"/>[/xml]

The reason for validatoin error is, an [highlight type=”special_text”]h:inputText[/highlight] can not bind to an object. That is the reason why it raises the validation error saying that, it can not convert a value using “null Converter”.

To solve this all you need is a custom Converter, which will convert the value from input text to a bean object. There are some tutorial regarding how to do it in JSF, but what about creating a custom converter using seam. Here is how u can do it.

  • Implement the Converter‘ interface and add the annotations as shown below.

[java]
package com.grandcorp.service.converter;

import java.io.Serializable;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Converter;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.grandcorp.entity.ItemIndentMast;
import com.grandcorp.service.ItemIndentMastServiceLocal;

@Name(value = "itemIndentMastConverter")
@Converter
@BypassInterceptors
public class ItemIndentMastConverter implements javax.faces.convert.Converter, Serializable {

private static final long serialVersionUID = 1L;

private ItemIndentMastServiceLocal itemIndentMastService;

public ItemIndentMastServiceLocal getItemIndentMastService() {
return itemIndentMastService;
}

public void setItemIndentMastService(ItemIndentMastServiceLocal itemIndentMastService) {
this.itemIndentMastService = itemIndentMastService;
}

@Override
public ItemIndentMast getAsObject(FacesContext facesContext, UIComponent uiComponent, String indentNumber) {
List<itemindentmast> filterIndent = itemIndentMastService.filterIndent(indentNumber);
if (filterIndent != null && filterIndent.size() == 1) {
return filterIndent.get(0);
}
return null;
}

@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object indent) {
if (indent != null) {
return indent.toString();
}
return null;
}

}
[/java]

  • Now use the converter using [highlight type=”special_text”]f:converter[/highlight] in your [highlight type=”special_text”]h:inputText[/highlight] component as shown below.

[xml]
<h:inputText value="#{lcInfo.itemIndentMastBean}" id="indentBean" required="true">
<f:converter converterId="itemIndentMastConverter"/>
</h:inputText>
[/xml]

Additionally you may want to inject any dependency. You can do so via [highlight type=”special_text”]components.xml[/highlight] like as below I did for [highlight type=”special_text”]private ItemIndentMastServiceLocal itemIndentMastService;[/highlight].

[xml]
<component name="itemIndentMastConverter">
<property name="itemIndentMastService">#{itemIndentMastService}</property>
</component>
[/xml]

So, this is it. Please make a comment if you find it difficult to implement.

By | 2011-09-03T18:23:40+00:00 September 3rd, 2011|java-ee|1 Comment

About the Author:

2 hand 2 leg 2 ear and 2 eyes... i am a man who is living to live...

One Comment

  1. GOzzie April 5, 2012 at 8:18 PM - Reply

    Hi, looks like a great tutorial except that I didn’t get it working from the beginning. The @Converter annotation and the library which it requires importing, org.jboss.seam.annotations.faces.Converter seems to conflict with javax.faces.convert.Converter

    Did you make this on Seam 2.x or Seam 3 ?

    Thanks

Leave a Reply