首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JFS中刷新<h:head>脚本?

如何在JFS中刷新<h:head>脚本?
EN

Stack Overflow用户
提问于 2013-07-26 13:39:20
回答 1查看 511关注 0票数 0

早上好

我正在使用Google中的Web通知--问题是:我的xhtml页面中有一个脚本,它通过托管bean从数据库中检索值​​,这个过程必须每10秒在一个计时器(间隔)上完成,第一次正常工作,但有时不返回数据库中的以下值--​​,而是继续显示恢复第一个​​的值--这是我可以做的第一次恢复时间间隔--值​​从托管bean数据库中再次恢复--我的代码希望这样做。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-26 14:57:12

普通的javascript正在客户端执行。

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

因此,您需要调用服务器来更新"nroCasosPendientes“和"nroRecorPendientes

你可以通过以下方式实现这一点:

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

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

代码语言:javascript
复制
<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>中,您需要做一些更改:

代码语言:javascript
复制
    <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的值将从隐藏字段中提取
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17882847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档