JSF页面在构造函数之前加载数据

时间:2012-09-20 09:44:56

标签: jsf-2 managed-bean

我创建了一个NotificationHandler类,这个类包含一个静态List。 为了允许访问此列表,我创建了一个SessionScopped bean,其中getter返回此列表。

我的问题如下:

我在页面A上,我在列表中放了一个通知,页面A被重新生成,正在显示通知。

然后我点击第A页的链接,将我发送到第B页。

在页面B中有一个构造函数,它会破坏列表。 但是,JSF页面在调用构造函数之前显示通知。

我最后在第B页上看到了第A页的通知

通知处理程序:

package com.cog.util;

import java.util.ArrayList;
import java.util.List;

public class NotificationHandler {

private static List<Notification2> listeDeNotification = new ArrayList<Notification2>();

public static void flushNotification(){
    listeDeNotification.clear();
}

public static void raiseNotification(NotificationColor color, String code){
    listeDeNotification.add(new Notification2(color.toString(), code));
}

public static void raiseNotification(NotificationColor color, String code, String additionalMessage){
    listeDeNotification.add(new Notification2(color.toString(), code, additionalMessage));
}

public static void addAdditionalMessage(String code, String additionalMessage){
    for(Notification2 n : listeDeNotification){
        if (n.getCode().equals(code)){
            n.setAdditionalMessage(additionalMessage);
        }
    }
}

public static Integer getNotificationNumber(){
    return listeDeNotification.size();
}

public static List<Notification2> getListeDeNotification(){
    return listeDeNotification;
}

}

notificationPrinter.xhtml

<composite:interface>
    <composite:attribute name="liste" />
    <composite:attribute name="locale" />
</composite:interface>

<composite:implementation>
    <ui:repeat value="#{cc.attrs.liste}" varStatus="status" var="l">
        <h:panelGrid width="100%" columns="1">
            <div id="alert" class="alert #{l.color}">
                <button class="close" data-dismiss="alert" type="button">×</button>
                <span class="rt">#{cc.attrs.locale[l.code]} #{l.additionalMessage}</span>
            </div>
        </h:panelGrid>
    </ui:repeat>
</composite:implementation>

AccessorBean

package com.cog.web.beans.util;

import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import com.cog.util.Notification2;
import com.cog.util.NotificationHandler;


@ManagedBean(name = "notificationAccessor")
@SessionScoped
public class NotificationAccessorBean {

    public List<Notification2> getNotificationList(){
        return NotificationHandler.getListeDeNotification();
    }

}

这就是我在jsf中的内容

问题是我想在某些页面之间刷新通知列表。

1 个答案:

答案 0 :(得分:1)

由于您使用的是JSF,您可以利用它的优势(h:commandLink for example),您也可以使用preRenderView尝试将其放入您的页面并使用其listener来刷新列表

查看以下说明/示例JSF 2 PreRenderViewEvent example

相关问题