在JSF / Primefaces中显示脏区(自定义警告消息)对话框

时间:2017-07-28 04:56:20

标签: jsf primefaces jsf-2

我正在研究primefaces应用程序,

如果页面中的某些内容发生了变化,并点击了另一个页面而没有保存,我需要显示一个弹出对话框,说明嘿,你改变了什么,你想要保存它,基于用户选择它应该保存/取消/无功能。

我看到了链接, to track the changes in JSF

如果它在同一个bean类中并单击了取消按钮,我可以编写一些函数并显示该对话框。

但是,如果用户点击其他链接/页面或菜单项如何处理这种情况。

无论如何,在流程转到任何其他bean之前,应该调用最后一个方法。

由于安全问题,我搜索时没有显示自定义警告消息。

@PreDestroy
public void destroy() { ..}

我尝试了这个,但是如果我每次点击其他链接都没有正确调用此方法。还有其他办法吗?

1 个答案:

答案 0 :(得分:2)

要捕获应用程序中的更改,您可能需要引入脏标志,如果任何组件值发生更改,则应用程序应将脏标志值设置为true。如下例所示。

<h:inputText id="someId" onchange="setDirty();"
    value="#{myManager.name}" maxlength="100">

用户尝试导航下一页而不保存,然后WindowBeforeUnloadListener将首先触发,只需检查布尔变量,如果值为true,则显示验证弹出窗口。

var dirty = true;
window.onunload = windowUnloadListener;
window.onbeforeunload = windowBeforeUnloadListener;
window.onload = windowLoadListener;

/*
 * Listener for Window-Onbeforeunload event.
 * Called before unloading (leaving the current page) the window.
 * This function checks for the dirty flag and pops up the 
 * message to confirm wit the user. 
 */
function windowBeforeUnloadListener() {
   if(dirty) {
       return "You have used navigation controls that are not part of the page.\n\n 
       If you wish to end, click 'Leave this page'. "
       + "Your session will end here.\n\n"
       + "If you wish to continue to use then please click 'Stay on this page'.";
    }
}

在页面加载和保存时,将脏标志值设置为false。

/*
 * Listener for Window-Onload event.
 * Called every time a new page is loaded.
 * This function will reset the flag.  
 */
function windowLoadListener(){
    dirty= true;
}

注意:自定义验证消息在IE中不起作用。它显示默认的浏览器验证消息。