Preserving JSF messages after a redirect

狂风中的少年 提交于 2019-12-10 13:39:43

问题


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?


回答1:


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.




回答2:


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/




回答3:


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.



来源:https://stackoverflow.com/questions/7503113/preserving-jsf-messages-after-a-redirect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!