How to call a method in the bean when a JSF page is requested?
How to call a method in the bean when a JSF page is requested? Examp开发者_运维百科le 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?
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.
精彩评论