• Post Reply Bookmark Topic Watch Topic
  • New Topic

Backing Bean Constructors  RSS feed

 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to pass args to a Backing Bean's constructor? Perhaps some sort of controllor that manages creation?

Thank you in advaneced, I appreciate your time,
-D.P.
 
Peter Goldstein
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a managed bean situation you're reallly not supposed to have constructors that take arbitrary arguments. The container is supposed to manage bean creation which works best if the constructor is simple and standardized - it simplifies the configuration.

Arguments that would be in a constructor are typically passed into the managed bean using the managed-property elements in faces-config.xml, which invoke the appropriate setters. I'm hard pressed to come up with a real-life backing bean situation where you couldn't replace constructor arguments with use of managed properties.

That all said, it would certainly be possible to create a bean in Java code (say, inside the methods of another bean) that has a constructor that takes arbitrary arguments. You could then bind this bean instance into the framework using the ValueBinding API. I'm not sure to what extent this bean would then be "managed", but it may accomplish what you're looking for. But I would very much recommend that you investigate managed properties before going this route.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking for somthing more dynamic then that, but I found a solution. I just pass the parmeters with the request and retreive them from the HttpServletRequest
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been learning Spring and one of its core features is the BeanFactory, which can be configured in roughly the same way as JSF's managed beans. Spring's approach has some nice things missing from managed beans:
1. The ability to invoke specific constructor and pass it arguments.
2. The ability to create beans by invoke static or non-static methods of factory classes
3. The ability to derive or template parameters. So if you have several beans that need to have the same properties set with the same values, you can do this in one place.
4. The ability to specify "init" and "destroy" methods of the bean and have them invoked properly.

On the other hand, there are some things missing that occur in managed beans:
1. There is no notion like managed-bean-scope. You can indicate is a bean will be a singleton, but there is no notion of session.
2. There is no "param" scope for http parameters

Still the notions are so similar I daydream that they may converge in a best-of-breed sort of way. I mean, why reinvent the wheel, the underlying paradigm, dependency injection, should be given a standard implementation once and for all, no?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!