通过< h:message>重定向进行演示后保留FacesMessage在JSF中 [英] Preserving FacesMessage after redirect for presentation through <h:message> in 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
我提供信息:
<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属性时,它没有为会话设置,而是仅针对此请求(尽管它在Javadocs中说的是什么)。
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 =keepMessagesvalue =true/>
I put the following code in my header.xhtml
<c:set target="#{flash}" property="keepMessages" value="true" />
现在它适用于每一页,我不必每次在背景中都需要它豆。
你需要JSTL,不要忘记将以下内容放在你的xhtml标题中:
xmlns:c =http://java.sun.com/jsp/jstl/核心
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"
这篇关于通过< h:message>重定向进行演示后保留FacesMessage在JSF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!