在JSF中显示FacesMessage并重新加载页面 [英] Show FacesMessage and reload Page in JSF

查看:130
本文介绍了在JSF中显示FacesMessage并重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从JSF应用程序发送电子邮件。我需要它当我发送电子邮件时,显示一个FacesMessage确认操作并且浏览器重新加载到同一页面。我的代码显示了Face但不重新加载同一页面。

I am sending Emails From a JSF Application. I need it Such That When I Send the Email, a FacesMessage is Displayed Confirming the Action and the Browser reloads to the Same Page. The code I have Shows the Face but does Not reload the same page.

public String sendEmail(){
//Email Sending Code.....
 Transport.send(message);

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Successfull\n", "You have successfully sent an e-mail to : " + e_mail + ". \n Thank You"));

            return "Email?redirect-true";
}

这是我尝试过的,此代码重新加载页面但FacesMessage是未显示。

This is What I Have tried, this code Reloads the Page But the FacesMessage is Not Shown.

public String sendEmail(){
    //Email Sending Code.....
     Transport.send(message);

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Successfull\n", "You have successfully sent an e-mail to : " + e_mail + ". \n Thank You"));
                 FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest origRequest = (HttpServletRequest) context.getExternalContext().getRequest();
            String contextPath = origRequest.getContextPath();
            FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath + "/faces/Email.xhtml");
                return "Email?redirect-true";
    }

以下是发送电子邮件页面的代码:

And Here Is My Code for the Send Email Page:

    <h:form >
            <p:growl id="growl" showDetail="true"/>
            <p:panel header="Send Email" style=" width: 70%">
                <h:panelGrid columns="2"> 
                    <h:outputLabel value="E Mail Address: " />
                    <h:inputText value="#{mailing.e_mail}" style="width: 400px"/>

                    <h:outputText value="Subject: " /> 
                    <h:inputText value="#{mailing.subject}" style="width: 400px"/>

                    <h:outputText value="Message: " />
                    <h:inputTextarea value="#{mailing.mess}" style="height: 300px;width: 700px"/>
                    <h:panelGroup/>
                    <p:commandButton
                        value="Send Mail "
                        action="#{mailing.sendEmail()}"
                        ajax="false"/>
                </h:panelGrid> 
            </p:panel>
        </h:form>

请帮助

推荐答案

你应该设置 keepMessages true 。 java.net/nonav/docs/2.0/javadocs/javax/faces/context/Flash.htmlrel =nofollow> Flash 如果你想要对象通过重定向保留 FacesMessages Flash 范围是JSF2中引入的新范围。范围(以及随附的隐式EL对象 flash )适用于stuff的短期存储(意味着您要保留的任何内容,短期)。

You should set keepMessages to true on the Flash object if you want to preserve FacesMessages across a redirect. The Flash scope is a new scope introduced in JSF2. The scope (and the accompanying implicit EL object flash) is intended for short term storage of "stuff" (meaning anything you want to keep, for a short period).

重定向前请尝试以下行:

Try the following line before you redirect:

   FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);

上面的行设置必要的标记以确保您的邮件被保留。

The line above sets the necessary flag to ensure your message is preserved.

进一步阅读:

  • Max Katz's JSF Blog on Flash

这篇关于在JSF中显示FacesMessage并重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆