开发者

How to call a method in the bean when a JSF page is requested?

开发者 https://www.devze.com 2023-01-21 21:34 出处:网络
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()\".

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消