Fahim Parkar
Fahim Parkar

Reputation: 31627

How to call Java method in Javascript

I am creating web page using JSF 2.0.

What I want to do is, call one method setLastVisitedPage that I have in bean MyBean when everypage loads.

I can do this by simply writing

<h:body onLoad="#{MyBean.setLastVisitedPage()}"

however I would have to write in all pages and already there are 30-40 pages created. :(

In page as I have common heading, I have created one file called commonPage.xhtml. I was thinking to include that file here.

So I added below in commonPage.xhtml so that whenever any page is loaded, myScript.js gets called and I get alert as "i m here".

<h:outputScript name="js/myScript.js" target="head" />

and myScript.js has

onload = alert("i m here");

Whenever I open any new page, I am getting alert as "i m here".

Now my question is how can I add #{MyBean.setLastVisitedPage()} in javascript so that setLastVisitedPage() get called for every page?

Is there any alternative way? May be I am thinking in wrong way.

Upvotes: 0

Views: 1374

Answers (1)

Daniel
Daniel

Reputation: 37051

Since you are using JSF you better use the PreRenderViewEvent , like this

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


public void putLastVisitedPage(ComponentSystemEvent event){
}

take a look here JSF 2 PreRenderViewEvent Example

Upvotes: 1

Related Questions