This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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

IDE: NetBeans 7.1 - Java:1.7.0_2 - JSF 2.0/2.1 - Mojarra 2.1.3 - Primefaces 3.2 - GlassFish 3.1.2
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15657
    
  15

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: 15657
    
  15

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a method or a field it's always also a constructor call?
 
Similar Threads
Another spec question
can i call a consturctor from a static method?
Using the Keyword super
Call a backing bean's method through href
regarding this or super call in constructors