重定向后保留 JSF 消息
我有一个 JSF 页面(使用 MyFaces 2.0),它在第一次显示时收集一些数据。如果它找不到某些信息,它应该提供一条消息来说明这一点,并重定向回不同的页面。我尝试使用此处找到的解决方案 Preserving FacesMessage after通过
相关代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保存消息的代码在阶段结束后执行(请参阅 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.
我遇到了同样的问题,并没有使用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.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/