• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converter instances

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a .xhtml with following piece of code (assume the converter instance xccx for ComplexConverter is properly registered in faces-config.xml)

<h:outputText value="#{ComplexMath.sum}">
<f:converter converterId="xccx"/>
</h:outputText>
<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 .
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I misread it. Apparently got validators and converters crossed in my head. I'd have to RTFM.
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic