java - 通过 JS 调用 JSF 方法

标签 java javascript jsf primefaces

<分区>

我在 JS 代码中有一个 for 循环,我想用 JAVA 托管 bean 中编写的参数调用一个方法,该方法计算一个值并返回一个将在 JS 中使用的新值 注意:我在 xhtml 页面中使用 primefaces 并且可以放在手上显示数据

这就是我的 js 的样子

function updateMoneyValue(){
   var thetable; //the handsonTable
   for (var i =0 ; i < thetable.length ; i++)
      {
         var myNewValue = theBeanMethod (firstParam , secondParam);
      }
}

最佳答案

您可以使用 PrimeFaces remote command component (<p:remoteCommand>)。

RemoteCommand enables executing backing bean methods and do partial update triggered by custom client side script. This example demonstrates a use case where a certain part of a page can be lazily loaded on demand.

通过以下方式将其添加到 View 中:

<p:remoteCommand name="myRemote" actionListener="#{myBean.listen}"/>

然后像这样在 Javascript 中使用它:

<script type="text/javascript">
   myRemote(); //makes a remote call
</script>

或者像这样从事件处理程序中调用它:

<div onclick="myremote();">...</div>

如果您还想将参数传递给服务器,请进行以下调用:

<script type="text/javascript">
   myRemote([{name:'param1', value:150}, {name:'param2', value:220}]); //makes a remote call with parameters
</script>

听众可能是这样的:

public void listen(){
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String,String> params = context.getExternalContext().getRequestParameterMap();
    System.out.println(params.get("param1"));
    System.out.println(params.get("param2"));
}

关于java - 通过 JS 调用 JSF 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920198/

相关文章:

java - <h :commandButton> doesn't start the action - NO IDEA?

java - 从 String 转换为具有大量值的 Java 枚举

java - Eclipse PDE 插件导出

javascript - 如何在 Backbone 中获取单个模型?

jsf - FacesExceptionFilter OmniFaces 使用 WEB-INF 内的错误页面

java - 我如何隐藏 f :selectItem tag under h:selectOneRadio

java - 检查firebase实时数据库用户名是否存在

java - 创建 json 对象

javascript - Eslint 报告依赖项配置的 eslintrc 配置问题

javascript - 如何使用 Vue Composition API/Vue 3 观察 Prop 变化?