重定向后保留 JSF 消息

发布于 2024-12-05 15:35:58 字数 843 浏览 0 评论 0原文

我有一个 JSF 页面(使用 MyFaces 2.0),它在第一次显示时收集一些数据。如果它找不到某些信息,它应该提供一条消息来说明这一点,并重定向回不同的页面。我尝试使用此处找到的解决方案 Preserving FacesMessage after通过重定向演示在 JSF(setKeepMessages(true)) 中,但重定向后消息未显示。我可以看出的唯一区别是我没有使用导航规则,我在外部上下文上调用了redirect() 调用,因为这不会在正常操作中发生。

相关代码:

public void redirectToPageWithMessage(String inPage, String message, FacesMessage.Severity severity){
    getFlash().setKeepMessages(true);
    addMessage(message, severity);
    try {
        getFacesContext().getExternalContext().redirect(inPage);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

不幸的是,这似乎不起作用。重定向发生得很好,但是 <消息>>标签未显示该消息。重定向()发生的方式是否有什么不同阻止了它的工作?

I have a JSF page (using MyFaces 2.0) that does a bit of data gathering the first time it is displayed. If it can't find some information, it is supposed to provide a message to that effect and redirect back to a different page. I've tried using the solution found here Preserving FacesMessage after redirect for presentation through <h:message> in JSF (setKeepMessages(true)) but the messages aren't displaying after the redirect. The only difference I can pick out is that I'm not using a navigation rule, I'm calling the redirect() call on the external context because this isn't occurring in a normal action.

Relevant code:

public void redirectToPageWithMessage(String inPage, String message, FacesMessage.Severity severity){
    getFlash().setKeepMessages(true);
    addMessage(message, severity);
    try {
        getFacesContext().getExternalContext().redirect(inPage);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Unfortunately this doesn't seem to be working. The redirect happens just fine, but the < messages /> tag isn't displaying the message. Is there something different about the way redirect() happens that prevents this from working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身软味 2024-12-12 15:35:58

保存消息的代码在阶段结束后执行(请参阅 Flash.doPostPhaseActions(FacesContext) )。因此,预计它不起作用,但也许您可以在重定向之前调用 Flash.doPostPhaseActions 。注意这不是一个“干净”的解决方案,但它是可能的。

The code that saves the messages are executed after the phase ends (see Flash.doPostPhaseActions(FacesContext) ). So, it is expected it does not work, but maybe you can call Flash.doPostPhaseActions before the redirect. Note is not a "clean" solution, but it is possible.

分分钟 2024-12-12 15:35:58

我遇到了同样的问题,并没有使用ExternalContext.redirect()来解决它,而是使用你的操作的结果。

也就是说,我的按钮调用的操作返回一个 String (结果),它指示转到下一页的导航规则。通过该系统,消息得以保留。

I had the same problem and solve it not using ExternalContext.redirect() but to play with outcome for your actions.

That is to say, my action called by my buttons return a String (the outcome) which indicates the navigation rules to go to the next page. With that system, the messages are preserved.

死开点丶别碍眼 2024-12-12 15:35:58

JSFMessage 仅为处理实际请求而保留。使用重定向时会发出第二个请求,因此 JSFMessages 将丢失。 EL-Flash 是解决这个问题的一种方法。此示例应该有效: http://ocpsoft.com/java /persist-and-pass-facesmessages-over-page-redirects/

JSFMessages are kept only for the processing of the actual request. A second request is made when using redirect, so the JSFMessages will be lost. The EL-Flash is a way to work around this. This example should work: http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文