对于从JS传递参数p:remoteCommand(由primefaces提供),您可以使用:
remoteCommandFunctionName({name1:'value1', name2:'value2'});
Run Code Online (Sandbox Code Playgroud)
之后,如何将这组参数remoteCommand发送给支持bean?
我需要在HTML DOM load事件期间使用ajax执行JSF托管bean操作方法,类似于jQuery $(document).ready(function() { $.ajax(...) }).我只能在这个项目中使用JSF生成的JavaScript.有没有办法在原生JSF中做到这一点?我可以使用哪个事件或者我可以使用哪个JSF ajax函数?
我正在使用JSF 2.0,Facelets和PrimeFaces.
注意:虽然这个问题涵盖了大量Java代码片段的长文本信息,但它仅仅针对JavaScript/jQuery以及一些PrimeFaces的东西(只是<p:remoteCommand>),如开头的介绍部分所述.
我收到来自WebSockets(Java EE 7/JSR 356 WebSocket API)的JSON消息,如下所示.
if (window.WebSocket) {
var ws = new WebSocket("wss://localhost:8181/ContextPath/AdminPush");
ws.onmessage = function (event) {
jsonMsg=event.data;
var json = JSON.parse(jsonMsg);
var msg=json["jsonMessage"];
if (window[msg]) {
window[msg](); //It is literally interpreted as a function - updateModel();
}
};
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,event.data包含一个JSON字符串{"jsonMessage":"updateModel"}.因此,msg将包含一个字符串值updateModel.
在以下代码段中,
if (window[msg]) {
window[msg](); //It is literally interpreted as a JavaScript function - updateModel();
}
Run Code Online (Sandbox Code Playgroud)
window[msg]();导致与a关联的JavaScript函数<p:remoteCommand>被调用(后者又调用actionListener="#{bean.remoteAction}"与之关联的函数<p:remoteCommand>).
<p:remoteCommand …Run Code Online (Sandbox Code Playgroud) 使用CDI和JSF2时如何将HTTP请求参数注入bean中?
如何将JS值传递给组件中的属性嵌套标签?
我有这个代码:
<p:remoteCommand .....>
<f:attribute name="galaxie" value="jstest()" />
</p:remoteCommand>
Run Code Online (Sandbox Code Playgroud)
而我的简单JS jstest功能:
function jstest(){
return "foo";
}
Run Code Online (Sandbox Code Playgroud)
当我在支持bean中测试galaxie的属性值时,我jstest()没有foo.