重定向后保留 FacesMessage 以通过 <h:message> 进行演示在 JSF [英] Preserving FacesMessage after redirect for presentation through &lt;h:message&gt; in JSF

查看:25
本文介绍了重定向后保留 FacesMessage 以通过 <h:message> 进行演示在 JSF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我认为的常见问题:一些托管 bean 有一个动作,可以将一些消息添加到上下文中:

I have what I suppose is a common problem: some managed bean has an action which adds some messages to the context:

FacesMessage fm = new FacesMessage("didn't work");
fm.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, fm);
return "some-outcome";

然后我将结果映射到 faces-config.xml 并将其配置为

Then I map the outcome in faces-config.xml and configure it to

<navigation-case>
    <from-outcome>some-outcome</from-outcome>
    <to-view-id>/view.xhtml</to-view-id>
    <redirect/>
</navigation-case>

view.xhtml 中,我显示了消息:

In view.xhtml I present the message:

<h:message globalsOnly="true" />

然而,它不起作用,因为执行重定向时消息丢失了.

However, it does not work because the message is lost when the redirect is executed.

我该如何解决?我发现这个 惊人的帖子解释如何使用 PhaseListener 做到这一点,但我相信这种情况太常见了,不能以这种方式解决.我错了吗?我应该创建 PhaseListener 吗?或者还有其他一些标准的解决方案吗?

How would I solve it? I found this amazing post explaining how to do it using a PhaseListener but I believe this situation is too common to have to be solved this way. Am I wrong? Should I create the PhaseListener? Or is there some other, standard solutions?

推荐答案

BalusC 一如既往的出色回答!

Great answer from BalusC as usual!

我只想补充一点,当我使用他的代码设置 keepMessages 属性时,它不是为会话设置的,而是仅为此请求设置的(尽管它在 Javadoc 中是这样说的).

I just want to add, when i used his code to set the keepMessages property, it was not set for the session, but only for this request (despite what it says in the Javadocs).

我将以下代码放在我的 header.xhtml 中<c:set target="#{flash}" property="keepMessages" value="true"/>

I put the following code in my header.xhtml <c:set target="#{flash}" property="keepMessages" value="true" />

现在它适用于每个页面,无需每次在支持 bean 中需要时都进行设置.为此,您需要 JSTL,并且不要忘记将以下内容放入您的 xhtml 标头中:xmlns:c="http://java.sun.com/jsp/jstl/core"

Now it works on every page, without me having to set it every time i need it in the backing bean. You need JSTL for this and dont forget to put the following in your xhtml header: xmlns:c="http://java.sun.com/jsp/jstl/core"

这篇关于重定向后保留 FacesMessage 以通过 <h:message> 进行演示在 JSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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