【问题标题】:Evaluate javascript result in jsf/primefaces component评估 jsf/primefaces 组件中的 javascript 结果
【发布时间】:2013-11-05 12:54:35
【问题描述】:

我想知道,如何在 jsf/primefaces 和 javascript 之间建立连接。

我想在用户键入空格后在我的支持 bean 中调用一个侦听器。 为了确定按下了哪个键,我得到了一个 javascript funktion

<script type="text/javascript">
   function displayunicode(e){
   var unicode=e.keyCode? e.keyCode : e.charCode
   return unicode
}
</script>

如果按下“空格”,则 'unicode' 的值为 32。

现在我要从带有“onkeyup 事件”的 primefaces 组件调用此函数

<h:form>
...

<p:inputTextarea id="inputBeschreibung" 
                                    value="#{bean.value}"
                                    rows="10" 
                                    cols="50"
                                    queryDelay="300" 
                                    maxlength="500"
                                    onkeyup="displayunicode(event); this.select()">

                    </p:inputTextarea> 

...
</h:form>

我不想在我的 primefaces 组件中使用这样的东西:

if(displayunicode(event); == 32) callBacking bean 方法。

我不知道该怎么做。

【问题讨论】:

    标签: javascript jsf primefaces


    【解决方案1】:

    从 JavaScript 到 bean

    要从 JavaScript 触发支持 bean 方法,您可以使用 p:remoteCommand 组件:https://www.primefaces.org/showcase/ui/ajax/remoteCommand.xhtml

    这是来自PrimeFaces user's guide的示例:

    <p:remoteCommand name="increment" actionListener="#{counter.increment}"
    out="count" />
    
    <h:outputText id="count" value="#{counter.count}" />
    
    <script type="text/javascript">
        function customfunction() {
            increment(); //makes a remote call
        }
    </script>
    

    从 bean 到 JavaScript

    如果你想要相反的动作,你可以使用 org.primefaces.context.RequestContext 类中的 execute(...) 方法:

    RequestContext context = RequestContext.getCurrentInstance();
    context.execute("alert('hello from bean');");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2012-01-21
      • 2011-07-17
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多