How to call a method in the bean when a JSF page is requested? Example I have a JSF page "MyPage.jsf" and its backend bean "MyBean" and it has a method "myMethod()". Is it possible to call the MyBean.myMethod() when MyPage.jsf is requested?
+2
A:
If you have added MyBean in the faces-config.xml as a managed bean:
<managed-bean>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
When you use the Bean in your MyPage.jsf example:
<h:outputText value="#{myBean.mytext}"/>
The default constructor of MyBean will automatically be called. Execute myMethod() from the default constructor.
public MyBean() {
this.myMethod();
}
And it will get called on page load.
Mark Robinson
2010-10-21 15:11:26
Thanks Mark, this worked!
Abhishek
2010-10-21 15:42:04