首页 > 解决方案 > 在命中服务器端业务逻辑之前捕获刷新事件

问题描述

这就是我在页面中实现 Flash 范围以确保 bean 在重定向后可用的方式。我正在使用 JBoss EAP 6 附带的 JSF 2.1.19。

page1.xhtml

<h:commandLink value="#{result.itemVO.number}" action="#{itemBean.goToSecondPage}">
    <f:setPropertyActionListener target="#{itemBean.itemVO}" value="#{result.itemVO}" />
</h:commandLink>

ItemBean.Java
这是两个页面之间的通用 bean。

public String goToSecondPage() {
    Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
    flash.put("itemBean", this);
    flash.setKeepMessages(true);
    flash.keep("itemBean");
    return "secondPage";
}

@PostConstruct
public void init() {
    Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
    ItemBean itemBean = (ItemBean) flash.get("itemBean");
    //Number
    itemBean.getItemVO.getNumber();
}

从 page1 重定向后,itemBean 对象在 init 方法中的 flash 中可用。

但是当我在第 2 页重新加载页面时,即使我调用 setKeepMessages(true),flash 也是空的。我根据 itemVO 在页面上加载了很多字段,但在重新加载后不存在并且页面呈现为空白。要么我想捕获重新加载事件并停止呈现页面,要么我需要找到一种方法来在重新加载的情况下保留 Flash 对象。

我已经通过这篇文章在重新加载后保留 flash 对象,但它谈到了 EL 的使用。但在我的情况下,我需要在 bean post 构造方法中访问它。

我还尝试了导航计时 api 来捕获 javascript 中的刷新事件,如此所述。但这是在 bean 初始化之后调用的,由于一些遗留代码问题,我想避免这种情况。

标签: javascriptjqueryservletsbrowserjsf-2

解决方案


推荐阅读