Steven De Groote
Steven De Groote

Reputation: 2233

Close primefaces dialog and redirect main page

In a submit handler of a button, I sometimes have to close my p:dialog and immediately perform of redirect of the main page. I have tried below code

RequestContext.getCurrentInstance().closeDialog(id);
FacesContext.getCurrentInstance().getExternalContext()
        .redirect("../quote/select.xhtml?prospectid=" + id);

This is contained in submit(), called via a simple commandButton:

<p:commandButton value="#{msg.common_save}" action="#{bean.submit}" />

Unfortunately, this does the redirect within the dialog, and does not close the dialog itself. Using only the first line does close my dialog, but then of course I still need to redirect my page.

Is there any way to do what I'm looking for?

Upvotes: 3

Views: 11284

Answers (2)

Abuzar Shaikh
Abuzar Shaikh

Reputation: 11

if you are still looking for the answer then I think I have done the same thing that you need. Excuse Me for bad formatting

here is the code for editor.xhtml file:

 <!DOCTYPE html>

  <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:x="http://java.sun.com/jsf/composite">
 <f:view>

<h:head>
    <title>Editor and dialog!</title>
</h:head>
<h:body>
    <h:panelGrid columns="1" cellpadding="5">

        <p:commandButton value="Edit Content" type="button" onclick="dlg2.show();" style="color:green;font-size:10px;"/>


    </h:panelGrid>

    <p:dialog header="Modal Dialog" widgetVar="dlg2" modal="true" >
        <h:outputText value="Edit Content here" style="color:green"/>
        <h:form>
        <p:growl id="message" showDetail="true" />
        <p:editor value="#{editorBean.value}" widgetVar="editContent" ></p:editor>
        <p:commandButton value="Publish"  action="editor?faces-redirect=true" update="message" onclick="dlg2.hide();">
            <p:confirmDialog header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
        </p:commandButton>


        </h:form>
    </p:dialog>   
    <div >
        <h:outputText  value="#{editorBean.value}"></h:outputText>
    </div>


</h:body>
  </f:view>
 </html>

And this is the code for the bean class I have named it as EditorBean.java

package com.dev;

import javax.faces.bean.ManagedBean;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.CloseEvent;


@ManagedBean(name="editorBean")
public class EditorBean {  

  private static String value;  
  //private static String outValue;


public EditorBean() {


}

public void addMessage(String summary, String detail) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
    FacesContext.getCurrentInstance().addMessage(null, message);
}


public String getValue() {  
    System.out.println("getValue()"+value);
    return value;  
}  



public void setValue(String value) { 
    System.out.println("setValue()"+value);
    addMessage("System Error", value+"! Please try again later.");
    this.value = value;  
    //getOutValue();
}

/*public String getOutValue() {
    this.outValue = value;
    System.out.println("getOutValue()"+outValue);
    return outValue;
}*/




}  

Upvotes: 1

David Goshadze
David Goshadze

Reputation: 1437

Ok If I get you correctly you are calling your bean through action property. This is not ususal way to go. Check the following example:

<p:dialog widgetVar="dlg" modal="true" resizable="false" header="Dialog">
    <!-- Dialog controls here -->
    <!-- ... -->

    <h:panelGroup>
    <p:commandButton value="Close and redirect" actionListener="#{bean.closeListener}"  action="/main" />
    <p:commandButton value="Just close" onclick="PF('dlg').hide()"/>
    </h:panelGroup>
</p:dialog>

'actionListener' property is used to execute any code behind this event. 'action' property is used here to set dialog outcome - this can be .xhtml page name (you can omit extention) or it can be EL expression refering bean method returning String. In the example above rediretcion will go to main.xhtml page located in WebContent root.

In general, 'actionListener' is executed first, then outcome is evalueted and jsf view changed to evaluated page.

Upvotes: 1

Related Questions