1

I want to know how to call a method in the backing bean once the page loads.. Consider this scenerio Once the user login is successfull his details are shown in the welcome page. Say I have login.xhtml and welcome.xhtml I have managed beans LoginController and DetailsController,Both are in SessionScope

There is a method called getDetails() in DetailsController

calling the method from the constructor would not be recommended method since I have read from DB in getDetails()

How do I call it on when the user is being redirect to welcome.xhtml where once he s redirected he should be able to see his details..

Thanks:)

DataNucleus
  • 15,199
  • 3
  • 30
  • 37
enthusiastic
  • 1,682
  • 4
  • 19
  • 31

3 Answers3

5

Check out the events e.g. you can have a preRenderView event which calls a backing bean method when the view is rendered:

<f:metadata>
        <f:event type="preRenderView" listener="#{myBean.preRender}"/>
</f:metadata>

Then get the data you want in scope via the preRender method on your bean. Typically this method would read data from a database and set in an appropriate scope e.g. View Scope. Your Value Bindings e.g. on outputText or inputText components would then just bind to the Objects you created or modified via the preRenderView event.

planetjones
  • 11,699
  • 3
  • 47
  • 51
0

The normal way would be for this method to be called by some value expression.
Say for instance, that you would have the #{myBean.details.price} expression; this would invoke getDetails on your backing bean.

Usually, you'll want to make sure that you only get the details from db once, if multiple expressions refer to it. This is easily done through a private field.

Another technique would be to use @PostConstruct. This only works if you use CDI (or spring). This will invoke a method after the constructor, but before any other usage. Keep in mind that this method will be called, not when the page is viewed, but when the bean is constructed. For a request-scoped bean that's the same thing. For session-scoped it is not.

Joeri Hendrickx
  • 15,619
  • 4
  • 37
  • 53
-1

make Save method in controller that returned String

public String Save() throws DaoException
{
    put your operation code here

    return "/template/template.xhtml&faces-redirect=true";
}
Jigar Patel
  • 386
  • 2
  • 8