Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18272
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answer.
If you do not recommend, how do you sort out a submit for example?
thanks again.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18272
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the explanation
Massimo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic