java 在 JSF 中通过 <h:message> 重定向展示后保留 FacesMessage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5137601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:38:14  来源:igfitidea点击:

Preserving FacesMessage after redirect for presentation through <h:message> in JSF

javajsfredirectjsf-2

提问by brandizzi

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

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

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

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

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

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

In view.xhtmlI present the message:

view.xhtml我提出消息:

<h:message globalsOnly="true" />

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

但是,它不起作用,因为执行重定向时消息丢失。

How would I solve it? I found this amazing postexplaining how to do it using a PhaseListenerbut 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?

我该如何解决?我发现这篇很棒的帖子解释了如何使用 a 来做到这一点,PhaseListener但我相信这种情况太常见了,不能用这种方式解决。我错了吗?我应该创建PhaseListener吗?还是有其他一些标准的解决方案?

采纳答案by styx

Great answer from BalusC as usual!

BalusC 像往常一样给出了很好的回答!

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).

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

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

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

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"

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

回答by heronsanches

JSF 2.2

JSF 2.2

Into the bean:

入豆:

FacesContext facesContext = FacesContext.getCurrentInstance();
Flash flash = facesContext.getExternalContext().getFlash();
flash.setKeepMessages(true);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "yourMessage", null));
return "namePage?faces-redirect=true"; 

Into the namePage

进入姓名页

Change

改变

<h:message globalsOnly="true" />

to

<h:messages globalsOnly="true" />