*
The moose likes JSF and the fly likes Calling a method or a field it's always also a constructor call? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Calling a method or a field it Watch "Calling a method or a field it New topic
Author

Calling a method or a field it's always also a constructor call?

massimo tarantelli
Ranch Hand

Joined: Jun 19, 2012
Posts: 35

I know that could be a very basic questions, anyway I had this doubt.
if I call a method or a field through the jsf pages using EL, (#{bean.method} for example), the calling will always pass through the bean's constructor?
or it will depend, for example, from the bean's scope?
thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

As a general rule, I do not recommend "calling" methods in EL. It's hard to debug and it breaks the MVC contract.

In the case of EL property references, the constructor is only called when the backing bean is first requested but does not exist. It is called once and only once - when the bean is built.

How often the bean is built depends on its scope. Request-scope beans get destroyed at the end of each request. View-scope beans get destroyed when you navigat to a different view. Session and application-scope beans have to be explicitly removed from their respective containers.


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

Joined: Jun 19, 2012
Posts: 35

thanks for the answer.
If you do not recommend, how do you sort out a submit for example?
thanks again.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

JSF will automatically validate the form inputs. If any of them are invalid, the form submit is rejected. Otherwise, JSF will automatically update the backing bean properties indicated by the "value=" attributes of the form's controls. If the commandButton or commandLink specifies an action method (or listener), that method will be invoked after the properties have been updated, so the action method will already have the input values directly available in the backing bean's member variables.
massimo tarantelli
Ranch Hand

Joined: Jun 19, 2012
Posts: 35

thanks for the explanation
Massimo
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling a method or a field it's always also a constructor call?
 
Similar Threads
Another spec question
Using the Keyword super
Call a backing bean's method through href
regarding this or super call in constructors
can i call a consturctor from a static method?