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

Why doesn't my action method work?  RSS feed

 
Denise Smith
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code goes into the method but the page does not send to the navigational rule.

I'm trying to send back to the sending page if a record set is empty(after setting a Session message) How are other people doing this?

faces-config has the rule.

Does this have to do with the phase i'm in???

This does not work either:
FacesContext.getCurrentInstance().getExternalContext().dispatch("/nasSearch.jsp");
[ September 15, 2006: Message edited by: Bear Bibeault ]
 
A. Dusi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your problem is with the display of messages, then this might help:
JSF error message
When you are in action method, you are in invoke application phase and cannot set any messages in this phase.
 
Chris Schalk
author
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Denise,

My guess is you may have an error that you're not seeing because you don't have a h:messages tag on your page. Make sure you add one.

A. Dusi,

Not sure what you mean about not being able to add Faces msgs in action methods. While true you are in the invoke application phase during an action method, but you can still add messages to the queue..

For example here's a quick login action method example:

public String login_action() {
if (inputText1.getValue().equals("chris") && inputSecret1.getValue().equals("schalk")){
return "success";
}
else {

FacesContext ctx = FacesContext.getCurrentInstance();
ctx.addMessage(null, new FacesMessage("Bad Login!"));
return null;
}

If the two input fields don't equal "chris" "schalk", then I create a FacesMessage and don't navigate.

Hope this helps..
-Chris
 
A. Dusi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,

I am little confused here. This link says that I cannot queue new messages in invoke application phase. And I have experienced the same; the messages were ignored when I add them in the action methods. Hence I always thought that a backing bean validator method is the best place for all the validation logic. Could you please elaborate on when a message is queued in this phase, should it be displayed if the same page is rerendered? What is the purpose of adding it in this phase?

Thanks in advance.
 
Chris Schalk
author
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by A. Dusi:
Chris,

I am little confused here. This link says that I cannot queue new messages in invoke application phase. And I have experienced the same; the messages were ignored when I add them in the action methods. Hence I always thought that a backing bean validator method is the best place for all the validation logic. Could you please elaborate on when a message is queued in this phase, should it be displayed if the same page is rerendered? What is the purpose of adding it in this phase?

Thanks in advance.


Just because something is posted on the Internet doesn't mean it's correct.. The link you pointed to states something that is incorrect. It claims that... whenever you return from an action, "it's going to cause a requestDispatcher.forward() or httpServletRespones.sendRedirect(). Both of these cause a new FacesContext instance to be created, your queued message is hence lost. "

This is only partially right. The key thing to observe is that when you create a nav rule and you specify you want a redirect..

<navigation-rule>
<from-view-id>/page1.jsp</from-view-id>
<navigation-case>
<from-outcome>navigate</from-outcome>
<to-view-id>/page2.jsp</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>

Then httpServletRespones.sendRedirect() is used which causes the browser to initiate a new request and in this case a completely new trip through the lifecycle is done and the old message that got queued is gone. This part is correct.

However the JSF-FAQ is incorrect when it says that when requestDispatcher.forward() is used (this is the case when a nav rule does NOT use redirect), it will also create a new request and new FacesContext. This is wrong. Since its not a redirect navigation, i.e. the browser is not reissuing a new request, the current request remains valid and any queued messages WILL be displayed at the render response phase.

Hopefully that clears things up? This is an important point and in general, the recommendation to use a validation method is a good one.

-Chris
 
Denise L Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem isn't with the display of the message but rather with the redirect. I will but a error msg on the page that should be doing the redirect. It is the one that gets displayed instead of the one that I want the redirect to goto. Chris, in your opinion, what kind of errors could I be getting? This page works without the redirect on it. This is the only thing new that I have added and only so that if a record set is empty that the user could be directed back to the input page and be given a message letting them know that there are no search records that meet their requirement. Is there a different way to handle this?
 
Fintan Conway
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Denise L Smith:
My problem isn't with the display of the message but rather with the redirect. I will but a error msg on the page that should be doing the redirect. It is the one that gets displayed instead of the one that I want the redirect to goto. Chris, in your opinion, what kind of errors could I be getting? This page works without the redirect on it. This is the only thing new that I have added and only so that if a record set is empty that the user could be directed back to the input page and be given a message letting them know that there are no search records that meet their requirement. Is there a different way to handle this?


Hi Denise,

If you return null or an empty string "" from your action method - JSF will return you to the same page (rather than following navigation rules). If you do all of the following :
(a) use the code that Chris showed earlier to add a faces message.
(b) do NOT use redirect in your navigation rule.
(c) return a null or "" in your action method when no results are returned.
(d) ensure you have a <h:messages> tag on the submitting page.

Your application should now return to the submitting page, showing your message.

HTH,

Fintan
[ September 18, 2006: Message edited by: Fintan Conway ]
 
A. Dusi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
Things are much clarified now. Thanks so much for your time!

Denise,
Your question turned out to help me too. Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!