【问题标题】:Show dirty area (Custom warning message) dialog in JSF/ Primefaces在 JSF/Primefaces 中显示脏区(自定义警告消息)对话框
【发布时间】:2017-07-28 04:56:20
【问题描述】:

我正在开发 primefaces 应用程序,

如果页面中的某些内容发生了更改,并且在没有保存的情况下单击了另一个页面,我需要显示一个弹出对话框说,您更改了某些内容是否要保存,基于用户选择它应该执行保存/取消/无功能。

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

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

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

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

由于安全浏览器没有显示自定义警告消息,因此我进行了搜索。

@PreDestroy
public void destroy() { ..}

我试过这个,但是如果我每次都点击其他链接,这个方法就不会被正确调用。有没有其他办法?

【问题讨论】:

    标签: jsf primefaces jsf-2


    【解决方案1】:

    要捕获应用程序中的更改,您可能需要引入脏标志,如果任何组件值发生变化,则应用程序应将脏标志值设置为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 中不起作用。它显示默认的浏览器验证消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多