问题描述
我有page1,该按钮具有导航到page2的按钮,第2页添加了一些消息并导航回到page1.我想在第1页上显示这些消息.我尝试了许多解决方案,但没有任何效果.
I have page1 with button that navigates to page2, page 2 add some messages and navigates back to page1. I want display those messages on page1. I've tried many solutions, but nothing work.
示例代码page1.xhtml:
Sample code page1.xhtml:
<p:commandButton value="edit" action="#{bean1.edit}"/>
在托管bean中:
public String edit() {
return "page2?faces-redirect=true";
}
page2托管bean
page2 managed bean
@PostConstruct
private void postConstruct() {
Faces.getFlash().setKeepMessages(true);
Messages.addFlashGlobalError("cannot edit!");
Faces.navigate("page1?faces-redirect=true");
}
两个bean都具有视图作用域,并且两个页面的正文末尾都有<p:messages>
.
Both beans are view scoped and both pages have <p:messages>
at the end of body.
推荐答案
首先,我不完全确定@PostConstruct
是执行重定向的最佳位置.参见此.话虽如此,Google出现了此,看起来很合理.尝试在Facelets页面本身内使用页面顶部附近的preRender
事件标签进行重定向.干杯
First of all, I'm not entirely sure that the @PostConstruct
is the best place to perform a redirect. See this. That being said, google turned up this and it looks reasonable. Try redirecting within the facelets page itself with a preRender
event tag close to the top of the page. Cheers
这篇关于JSF-从@PostConstruct重定向后保留Faces消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!