wood burning stoves*
The moose likes JSF and the fly likes Converter instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Converter instances" Watch "Converter instances" New topic
Author

Converter instances

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
I have a .xhtml with following piece of code (assume the converter instance xccx for ComplexConverter is properly registered in faces-config.xml)

<hutputText value="#{ComplexMath.sum}">
<f:converter converterId="xccx"/>
</hutputText>
<h:inputText id="a" value="#{ComplexMath.a}">
<f:converter converterId="xccx"/>
</h:inputText>
<h:inputText id="b" value="#{ComplexMath.b}">
<f:converter converterId="xccx"/>
</h:inputText>
<h:commandButton value="Add" ></h:commandButton>

since 3 converter tags are associated with 3 components ,3 instances of converters are created and used. This i feel as unnecessary. can we create only one instance of converter and associate it with 3 uicomponents ? this i feel improves the performance .
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Are you asking if it is possible to define that a converter is "reusable" or are you asking why it is not implemented the way you described?

I think that making a few extra objects that have a very short lifespan is not such a big deal. And on the other hand since converters can do so much more in the background than just "format a field" (like contact a database or use object properties and so on) it is a much more safer to just recreate the object every time. But this is of course a rare case.

If you do want to use just one object it is possible. Just use the binding attribute in the f:converter tag!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Converters are stateless components. That means that instance variables are not really appropriate and therefore in theory, a single converter instance could be used for all 3 items. In current implementations, however, I believe 3 instances would be created. On the other hand, instantiation is a pretty low-overhead operation in current Java implementations.

If you absolutely, positively MUST have only one converter code instance, make a converter method in a backing bean and use that instead.

However, as we frequently caution people in our optimization forum, premature optimization is not a wise thing to do.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Tim Holloway wrote:If you absolutely, positively MUST have only one converter code instance, make a converter method in a backing bean and use that instead.

Is that possible? I was under the impression that doing that is only possible with validator. Or did you mean that the backing bean should implement Converter interface?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Maybe I misread it. Apparently got validators and converters crossed in my head. I'd have to RTFM.
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
If you do want to use just one object it is possible. Just use the binding attribute in the f:converter tag!


there is no binding attribute in <f:converter> tag.it has only converterId attribute
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
kish kumar wrote:
there is no binding attribute in <f:converter> tag.it has only converterId attribute

At least in JSF 2 there is
http://download.oracle.com/docs/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/pdldocs/facelets/f/converter.html

If there is no such attribute in JSF 1 then I do not have a solution to propose. Other than replacing the implementation of the f:converter but I think that is not worth the trouble!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converter instances
 
Similar Threads
JSF Validatuion to acquire only numeric values for an input field
creating a table component dynamically
Safari - TypeError on first submit
Using simultaneously Converters
<hx:inputHelperAssist> not working