Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Call a method after a <jsp:forward>  RSS feed

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

My first .jsp (index.jsp) does a <jsp:forward page="/pages/homes.faces"> to use the framework JSF.
Now, I need to use a bean to show some informations in the home page but I don't know how to call the method initPage() in my Bean when I do a <jsp:forward>.

Can somebody help me ?

PS: I see "<f:event>" but we don't use a recent library apparently, so.. it doesn't exist :-)

Thanks in advance.

Angy.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the scope of the backing bean that contains initPage(), you might be able to use the @PostConstruct annotation on it.
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work apparently.. :/
My constructor is called but not my method initPage() with the "PostConstruct" annotation.

 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@PostConstruct methods must return void: http://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html

Note that you cannot use PostConstruct to do JSF navigation. That's not what it's for.
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... that is annoying...
Is there another solution ?

Summary:
When I launch my application with an url (ex: http://localhost:8080/myApp).
A JSP is displayed (index.jsp) and in this JSP, I have a <jsp:forward page="/pages/homes.faces"> and this, executes another JSP "home.jsp" but without calling my method initPage() which has to call a method to populate a dataTable and my initPage() has to return a String for navigation.

I really have no idea how to do... :(

And I have question about <f:event>, if I had the recent library, could this problem solve ?

PS:
I'm trying with this, but the method is not called and the method returns void :/



 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that a more appropriate approach in a case like this is to make your lead-in JSP construct the backing bean in the old J2EE style, invoke the initPage method, and use the initPage response to construct the forwarding URL. You'd typically use scriptlets to do this, because although we generally discourage scriptlets these days, the actual amount of logic involved is small enough that it's not worth pairing the JSP with its own servlet when most of the actual logic would be in the initPage method anyway.

It's perfectly OK to create a session-scope java object in a JSP or servlet and then reference it as a JSF backing bean. JSF will neither know nor care who constructed the bean.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!