关闭PrimeFaces对话框并重定向到主页面。

3
在按钮的提交处理程序中,我有时必须关闭我的并立即执行主页面的重定向。我尝试了下面的代码。
RequestContext.getCurrentInstance().closeDialog(id);
FacesContext.getCurrentInstance().getExternalContext()
        .redirect("../quote/select.xhtml?prospectid=" + id);

这段代码包含在submit()函数中,通过一个简单的commandButton调用:

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

很不幸,这个方法只在对话框内部进行重定向,并不能关闭对话框。只使用第一行代码确实会关闭对话框,但是我还需要重定向我的页面。

有没有什么方法可以达到我想要的效果呢?


你能展示一下 XHTML 代码吗?不太清楚你是如何调用托管的 Bean。 - David Goshadze
1
编辑了一下,没有什么特别的,只是一个p:commandButton触发了一个ajax提交。请注意,我也尝试过使用h:commandButton,结果相同。 - Steven De Groote
如果你无论如何都要重定向,为什么还要在意你是否关闭了对话框呢? - Hatem Alimam
2
因为当你在打开对话框时进行重定向时,你只是重定向了对话框中显示的iframe。而这正是我在问题中所不需要的。 - Steven De Groote
2个回答

1
如果您仍在寻找答案,我认为我已经做了您需要的相同操作。请原谅我的格式不佳。
以下是editor.xhtml文件的代码:
 <!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>

这是我命名为EditorBean.java的bean类的代码。

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;
}*/




}  

1

如果我理解正确,您是通过操作属性调用您的bean。这不是通常的做法。请参考以下示例:

<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'属性用于在事件后执行任何代码。 'action'属性用于设置对话框结果,这可以是.xhtml页面名称(您可以省略扩展名),也可以是引用返回字符串的bean方法的EL表达式。 在上面的示例中,重定向将转到位于WebContent根目录中的main.xhtml页面。

通常情况下,'actionListener'首先被执行,然后评估结果并将jsf视图更改为评估的页面。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接