在 AJAX 调用之间保留 JSF 消息

发布于 2024-11-16 15:27:04 字数 173 浏览 2 评论 0原文

有一个 JSF 1.2 页面,其中包含 组件。当消息放置在 FacesContext 中时,它只会短暂显示,直到轮询发送下一个请求。是否可以跨 AJAX 请求保留消息?

There is a JSF 1.2 page with <rich:messages> and <a4j:poll> components on it. When a message is placed in FacesContext it is only shown briefly, until the poll sends the next request. Is it possible to preserve messages across AJAX requests?

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

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

发布评论

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

评论(2

风渺 2024-11-23 15:27:04

可能,但您需要自己保存消息。 JSF 中的消息是请求范围的,因此在请求之间会被清除。

Possible, but you would need to save the message yourself. Messages in JSF are request scoped, and thus cleared between requests.

等风来 2024-11-23 15:27:04

将以下内容添加到您的 Facelet:

<ui:define name="metadata">
  <f:event type="preRenderView" listener="#{ajaxBacking.displayMessages}" />
</ui:define>

创建一个支持 bean 以通过 ajax 调用保存消息:

@ManagedBean
@ViewScoped
public class AjaxBacking {

Map<String, List<FacesMessage>> messages;

public AjaxBacking(){
    messages = new HashMap<String, List<FacesMessage>>();
}

public static AjaxBacking getCurrentInstance() {
    AjaxBacking ajaxBacking = null;
    FacesContext context = FacesContext.getCurrentInstance();
    if ((ajaxBacking = (AjaxBacking) context.getViewRoot().getViewMap().get("ajaxBacking")) == null){
        ajaxBacking = (AjaxBacking)context.getApplication().evaluateExpressionGet(context,"#{ajaxBacking}",AjaxBacking.class);
    }
    return ajaxBacking;
}

public void displayMessages(){
    FacesContext facesContext = FacesContext.getCurrentInstance();
    for (String component : messages.keySet()){
        for (FacesMessage message : messages.get(component)) {
            if (!facesContext.getMessageList(component).contains(message)){
                facesContext.addMessage(component, message);
            }
        }
    }
}

public void putMessages(String component, List<FacesMessage> facesMessage){
    List<FacesMessage> messages = new ArrayList<FacesMessage>();
    for (FacesMessage message : facesMessage) messages.add(message);
    this.messages.put(component, messages);
}

public Map<String, List<FacesMessage>> getMessages() {
    return messages;
}
}

最后将此行添加到任何添加 FacesMessage 的 bean 方法的底部。

AjaxBacking.getCurrentInstance().putMessages(messages,facesContext.getMessageList(messages);

Add the following to your facelets:

<ui:define name="metadata">
  <f:event type="preRenderView" listener="#{ajaxBacking.displayMessages}" />
</ui:define>

Create a backing bean to persist the messages through ajax calls:

@ManagedBean
@ViewScoped
public class AjaxBacking {

Map<String, List<FacesMessage>> messages;

public AjaxBacking(){
    messages = new HashMap<String, List<FacesMessage>>();
}

public static AjaxBacking getCurrentInstance() {
    AjaxBacking ajaxBacking = null;
    FacesContext context = FacesContext.getCurrentInstance();
    if ((ajaxBacking = (AjaxBacking) context.getViewRoot().getViewMap().get("ajaxBacking")) == null){
        ajaxBacking = (AjaxBacking)context.getApplication().evaluateExpressionGet(context,"#{ajaxBacking}",AjaxBacking.class);
    }
    return ajaxBacking;
}

public void displayMessages(){
    FacesContext facesContext = FacesContext.getCurrentInstance();
    for (String component : messages.keySet()){
        for (FacesMessage message : messages.get(component)) {
            if (!facesContext.getMessageList(component).contains(message)){
                facesContext.addMessage(component, message);
            }
        }
    }
}

public void putMessages(String component, List<FacesMessage> facesMessage){
    List<FacesMessage> messages = new ArrayList<FacesMessage>();
    for (FacesMessage message : facesMessage) messages.add(message);
    this.messages.put(component, messages);
}

public Map<String, List<FacesMessage>> getMessages() {
    return messages;
}
}

And finally add this line to the bottom of any bean method that adds a FacesMessage.

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