0

早上好

我正在使用 Google Chrome 中的 Web API 通知问题是:我的 xhtml 页面头部有一个脚本,该脚本通过托管 bean 从数据库中检索值,并且这个过程必须在计时器(间隔)上完成每隔 10 秒,第一次它工作正常但有时不会在数据库中返回以下值,而是继续显示值以恢复第一次。我该怎么做才能使运行间隔越来越多的值​再次从数据库中恢复托管bean我的代码如下希望对您有所帮助。

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:ice="http://www.icesoft.com/icefaces/component"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ace="http://www.icefaces.org/icefaces/components">
<h:head>
   <script>

    if (!window.webkitNotifications) 
    {
        alert('Lo sentimos, su navegador no soporta notificación de escritorio. Trabaje con Google Chrome.');
    }

    function RequestPermission (callback)
    {
        window.webkitNotifications.requestPermission(callback);
    }

    function getNroCasosPendientes()
    {
        var nroCasosPendientes = '#{ControladorBk.getNroCasosPendientes()}';

        return nroCasosPendientes;
    }

    function getNroRecordatoriosPendientes()
    {
        var nroRecorPendientes = '#{ControladorBk.getNroRecordatoriosPendientes()}';

        return nroRecorPendientes;
    }

    function abrirVentana(url) 
    {
        var height = screen.availHeight-30;
        var width  = screen.availWidth-10;
        var left   = 0;
        var top    = 0;

        settings   = 'fullscreen=no,resizable=yes,location=no,toolbar=no,menubar=no';
        settings   = settings + ',status=no,directories=no,scrollbars=yes';
        settings   = settings + ',width=' + width +',height=' + height;
        settings   = settings + ',top=' + top +',left=' + left;
        settings   = settings + ',charset=iso-8859-1';
        var win    = window.open(url, '', settings);

        win.outerHeight = screen.availHeight;
        win.outerWidth  = screen.availWidth;

        win.resizeTo(screen.availWidth, screen.availHeight);

        if (!win.focus)
        {
            win.focus();
        }

        return win;
    }

    function notification ()
    {               
        if (window.webkitNotifications.checkPermission() > 0) 
        {
            RequestPermission(notification);
        }

        var icon               = 'http://entidades.com/images/img999.png';
        var title              = 'AVISO'; 
        var nroCasosPendientes = getNroCasosPendientes();
        var nroRecorPendientes = getNroRecordatoriosPendientes();

        if(nroCasosPendientes != '0' || nroRecorPendientes != '0')
        {
            var body               = 'Tienes '+nroCasosPendientes+' Casos y '+nroRecorPendientes + ' Recordatorios pendientes.';
            var popup              = window.webkitNotifications.createNotification(icon, title, body);
            popup.show();
            setTimeout(function()
            {
                popup.cancel();
            }, '5000');

            popup.onclick = function() 
            {
                abrirVentana('http://localhost:8080/Proyect/faces/Page.xhtml');
            };
        }       
     }

     var timer = setInterval(function() 
     {
        notification();
     }, 10000);

    </script>
</h:head>
4

1 回答 1

0

普通的 javascript 在客户端执行。

"#{ControladorBk.getNroCasosPendientes()}"像- 在服务器端的表达式 。

所以,你需要调用服务器来更新“ nroCasosPendientes”和“ nroRecorPendientes

您可以通过以下方式实现:

  1. 将值放入隐藏字段
  2. 通过 ajax 调用更新它们
  3. 在 javascript 中获取更新的值

因此,在 html 中,您需要添加以下代码:

<h:form>
    <h:inputHidden id="hiddenCasos"
        value="#{ControladorBk.getNroCasosPendientes()}"/>
    <h:inputHidden id="hiddenRecordatorios"
        value="#{ControladorBk.getNroRecordatoriosPendientes()}"/>

    <h:commandLink id="btnUpdate"
        style="display: none">
        <f:ajax render="hiddenCasos hiddenRecordatorios"
            onevent="performNotification"/>
    </h:commandLink>
</h:form>

如你看到的:

  1. 添加了两个隐藏字段,用于从 bean 中获取值
  2. 添加了用于执行 ajax 请求的不可见按钮

<script>你需要做一些改变:

    <script>
    //your code

    //add this...
    function updateBean() {
        document.getElementById("btnUpdate").click();
    }

    //...and this
    function performNotification(data) {
    if(data.status === 'success') {
        notification();
        }
    }

    //...change this
    function notification() {
        //your code

        //get values from hidden fields
        var nroCasosPendientes = document.getElementById("hiddenCasos").value;
        var nroRecorPendientes = document.getElementById("hiddenRecordatorios").value;
    }

    //...and change this
    var timer = setInterval(function{
        updateBean();
    }, 10000);
</script>

所以,变化是:

  1. 计时器现在将执行单击按钮,这将导致 ajax 请求
  2. 隐藏字段由 ajax 更新
  3. ajax成功后notification()会调用
  4. notification()nroCasosPendientesnroRecorPendientes将取自隐藏字段
于 2013-07-26T14:57:12.127 回答