我在 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"));
}