Convert classes

Asked

Viewed 74 times

0

I have studied programming in JSF, I read that for objects to be transformed into String you need a Convert to the entity.

Is there a class already ready and we adapt our Entities? How it works ?

1 answer

2


You can create a class that implements the interface Converter, in it we can define how the data conversion will take place, in this way you customize this conversion.

By steps: Create a class that implements the interface javax.faces.contert.Converter and is noted with @facesConverter. A converter is a class that can transform strings in objetos and objetos in strings, this class should implement the methods getAsObject() and getAsString();

The method getAsObject() must convert from String for objeto, in this case may cast an exception of the type ConverterException;

The method getAsString() must convert from objeto for String.

Answering your main question, when implementing Converter you only need to apply the conversion logic to the methods.

  • Show, thank you.

  • Cool: just a hint... take into account that these classes can swell your projects, so I advise you to separate them into separate packages. In the demos of Primefaces you have some cool examples.http://www.primefaces.org/showcase/ui/input/oneMenu.xhtml, go to ThemeConverter.java

  • My entities stay in the Model, where Converters should stay ?

  • Man, it depends. I usually put them in sub-packages from where my Beans are, but see how best would be understanding your Project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.