如何在页面导航之间处理JSF消息(POST-REDIRECT-GET) [英] How to deal with JSF messages between page navigations (POST-REDIRECT-GET)
问题描述
我正在开发 JSF 2 Web应用程序,并使用从页面到页面的隐式导航指定结果值和更改url值.我也有一些Ajax请求,以能够在某些页面中进行更改而不必提交所有内容.
I'm developing JSF 2 web application and using implicit navigation from page to page specifying outcome values and changing url values. I also have some Ajax requests to be able to make changes in some pages without having to submit everything.
例如,当我编辑用户的数据并保存更改时,就会出现我的问题.托管Bean保存用户数据并返回一个String以导航到用户列表.正确保存用户后,将在操作方法完成之前添加FacesMessage
.
My problem comes when, for example, I edit a user's data and save the changes. Managed Bean saves user data and return an String to navigate to the user list. When user is properly saved, a FacesMessage
is added before the action method finishes.
按钮
<p:commandButton value="#{msg.UPDATE}" action="#{manager.actionSave}"
ajax="false" />
编码到方法中
FacesContext.getCurrentInstance().addMessage(clientId,
new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
但是,即使我的主页上没有<h:messages />
标签,也没有任何显示.
However, and even I have an <h:messages />
tag in my main page, nothing is displayed there.
推荐答案
问题症状表明您正在通过重定向进行导航,而不是通过结果中的faces-redirect=true
参数或<如果您仍在使用faces-config.xml
中的旧版导航规则,则在导航案例中输入c5>条目.
The problem symptoms suggests that you're navigating by a redirect instead of a (default) forward by either the faces-redirect=true
parameter in the outcome, or the <redirect/>
entry in the navigation case, if you're still using legacy navigation rules in faces-config.xml
.
Faces消息在请求范围内,因此仅在当前请求所服务的资源中可用.通过重定向导航时,基本上是在指示Web浏览器在给定的URL上创建一个全新的请求.面部消息在该请求中根本不可用.
Faces messages are request scoped and are thus only available in the resource served by the current request. When navigating by a redirect, you're basically instructing the webbrowser to create a brand new request on the given URL. The faces messages are not available in that request at all.
如果重定向确实是强制性的,例如完成 POST-重定向-获取模式—这是一件好事&mdash ;,那么您需要将消息存储在Flash作用域中.您可以通过调用 Flash#setKeepMessages()
,并通过true
.
If redirecting is really mandatory, e.g. to accomplish the POST-Redirect-GET pattern — which is a Good Thing —, then you need to store the message in the flash scope. You can do that by calling Flash#setKeepMessages()
, passing true
.
context.addMessage(clientId, message);
context.getExternalContext().getFlash().setKeepMessages(true);
请注意,如果您使用的Mojarra版本低于2.1.14,并且将其重定向到其他基本文件夹中的资源,则此操作将失败.另请参见问题2136 .
Note that this will fail if you're using a Mojarra version older than 2.1.14 and are redirecting to a resource in a different base folder. See also issue 2136.
这篇关于如何在页面导航之间处理JSF消息(POST-REDIRECT-GET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!