Navigation from a p:dialog not working

UI Components for JSF
Post Reply
dwhite
Posts: 3
Joined: 28 Dec 2011, 16:46
Location: UK

28 Dec 2011, 19:18

I have a form where some Yes/No questions are answered and the form is submitted via a h:commandButton and validated against the expected result. A flash variable (wrongAnswer) is updated to reflect if the answers were correct and "success" or "failure" returned.

If "success" is returned a navigation rule fires and we move to another page.
If "failure" is returned a different navigation rule fires and we return to the same page (both these occur via a redirect).

In the case of a "failure" the page will render a modal dialog, which when the user clicks on "Close" should navigate to a third page (due to another navigation rule) however it still stays on the same page. Can anyone suggest why this might be, or suggest another way of achieving the same page flow?

I'm running:
JSF 2.0
Weblogic 10.3.5
PrimeFaces 2.2.1

Code: Select all

<h:form id="answerQuestions">
<h:commandButton id="answerButton" value="Answer" action="#{bean.answer}" />

<p:dialog header="" 
      closable="false"
      rendered="#{flash.wrongAnswer}"
      visible="true" modal="true" 
      widgetVar="dlg" resizable="false">  
      <h:outputLabel value="Sorry wrong answer" />
      <p:button id="modalClose"
		     value="Close"
		     action="failureAcknowledged"
	             ajax="false" onclick="dlg.hide();" />
</p:dialog>
</h:form>

    public String answer()
    {
        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        if(validateSelections())
            flash.put("wrongAnswer", false);
            return "success";
        }
        else
        {
            flash.put("wrongAnswer", true);
            return "failure";
        }
    }

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

28 Dec 2011, 22:39

I searched google for the following; hopefully, they can help you solve this issue. This is a question that should be posed to a larger JSF developer community instead of just PrimeFaces community.

balusc navigation case commandbutton
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

dwhite
Posts: 3
Joined: 28 Dec 2011, 16:46
Location: UK

29 Dec 2011, 23:06

Thanks for the reply smithh032772. I had a look at some of the articles returned by your Google search but didn't spot anything that helped me, but perhaps I've missed something.
I posted here because the ("failureAcknowledged") navigation that didn't work was from the p:button within the p:dialog, the h:commandButton navigations ("success" and "failure") both work as expected.

alepekhin
Posts: 71
Joined: 27 Dec 2011, 18:01
Location: Saint-Petersburg, Russia

29 Dec 2011, 23:36

I've solved the similar task as follows. First on the page add javascript

<script type="text/javascript">
function handleLoginRequest(xhr, status, args) {
if(args.hideDialog) {
yourDialogWidgetVar.hide();
}
}
</script>

and add to <p:commandButton> inside dialog attribute oncomplete="handleLoginRequest(xhr, status, args)"
Now your dialog window will be closed only if args.hideDialog == true.
The value of this variable can be managed from controller:

public void doSomething() {
try {
your code here
RequestContext.getCurrentInstance().addCallbackParam("hideDialog", true);
} catch (Exception e) {
RequestContext.getCurrentInstance().addCallbackParam("hideDialog", false);
}
}

Using <p:commandButton> is important. RequestContext.getCurrentInstance() will be null if you use <h:commandButton>. It works for ajax requests only.
PrimeFaces 3.2, Mojarra 2.1.6 , Tomcat 7.0, Eclipse Indigo, Java 7

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

29 Dec 2011, 23:39

Good. Thanks for letting us know. Someone else may benefit from your response/findings.
Last edited by smithh032772 on 29 Dec 2011, 23:47, edited 1 time in total.
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

alepekhin
Posts: 71
Joined: 27 Dec 2011, 18:01
Location: Saint-Petersburg, Russia

29 Dec 2011, 23:43

oh, I forgot to say, don't use redirect for returning to the same page. Simply stay there.
PrimeFaces 3.2, Mojarra 2.1.6 , Tomcat 7.0, Eclipse Indigo, Java 7

dwhite
Posts: 3
Joined: 28 Dec 2011, 16:46
Location: UK

03 Jan 2012, 17:14

Hello alepekhin,
Thanks for posting the code for your solution, it is much appreciated. Apologies for the delay in replying but I've been out of the office.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests