Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converter instances

 
kish kumar
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • 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)

<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
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17624
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 17624
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic