【问题标题】:JSF page loading data before constructorJSF页面在构造函数之前加载数据
【发布时间】:2012-09-20 09:44:56
【问题描述】:

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

我的问题如下:

我在页面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 中放入的内容

问题是我只想刷新一些页面之间的通知列表。

【问题讨论】:

  • 不确定,但听起来您应该使用 @ManagedProperty 从另一个 bean 访问一个 bean,这样您就可以在导航之前访问和刷新通知...
  • 不确定我是否可以这样做,因为 NotificationAccessorBean 中除了 getter 之外没有任何内容,我应该将 NotificationHandler 的内容放入 bean 中吗?
  • 当您在页面之间导航时,您引用了action 属性中的任何方法?如果是这样,它可能在某个 bean 中实现,因此在该 bean 中创建 NotificationHandler 的@ManagedProperty 并刷新通知(类似的东西)
  • 不,我使用的是常规链接:#{msg.RechercherUneGamme }。所以我不能调用任何方法来刷新。
  • 由于您使用 JSF,您可以利用它的优势 (h:commandLink for example),您也可以使用 preRenderView 尝试将它放在您的页面中并使用它的侦听器刷新列表 mkyong.com/jsf2/jsf-2-prerenderviewevent-example

标签: jsf-2 managed-bean


【解决方案1】:

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

看看下面的解释/例子JSF 2 PreRenderViewEvent example

【讨论】:

  • 是的,使用 preRenderView 工作,谢谢 =D 但是知道我发现了为什么会这样:richfaces,当它们在页面上没有richfaces 时,在通知绘制过程之前调用构造函数。跨度>
  • 欢迎您,尽管我不确定richfaces 的存在如何改变构造函数调用的顺序..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
相关资源
最近更新 更多