0

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.

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

let me try... If it looks beautiful, I will let you know :)
getting exception as javax.faces.FacesException: Service Tracker has not been initialized
don't know that was for something else... after re-compiling all working smoothly.. Thanks for help again...
Could you please look at this question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.