[英]Calling JSF managed bean method with arguments in jQuery
我正在使用JSF来构建一个站点。 我在我的主页上添加了jQuery Gritter(Growl)通知。 是否可以在$.gritter.add
函数的before_close:
中调用托管bean方法?
我想要使用的代码如下:
<h:body>
<c:forEach items="#{notificationBean.growlNotificationList}" var="p">
<script>
/* <![CDATA[ */
$.gritter.add({
// (string | mandatory) the heading of the notification
title: 'Notification',
// (string | mandatory) the text inside the notification
text: 'Comment on your staus',
// (bool | optional) if you want it to fade out on its own or just sit there
sticky: true,
// (int | optional) the time you want it to be alive for before fading out (milliseconds)
time: 8000,
// (string | optional) the class name you want to apply directly to the notification for custom styling
class_name: 'gritter-light',
// (function | optional) function called before it closes
before_close: function(e, manual_close){
'#{notificationBean.set0ToGrowlToShow(p.notificationID)}'
}
});
/* ]]> */
</script>
</c:forEach>
</h:body>
您当前的尝试只是将给定的EL表达式解释为值表达式,并在生成嵌入了JS代码的HTML输出时立即打印其结果。 就像你正在使用<h:outputText>
。 这确实无法奏效。
然而,理解功能要求。 标准JSF API不提供现成的解决方案。 如果您想坚持使用标准的JSF API,最好的办法是创建一个隐藏的表单,其中包含一个使用JavaScript触发的隐藏命令链接。
基本上,
<h:form id="form" style="display:none">
<h:inputHidden id="id" value="#{notificationBean.notificationID}" />
<h:commandLink id="command" action="#{notificationBean.set0ToGrowlToShow}">
<f:ajax execute="@form" />
</h:commandLink>
</h:form>
同
$("[id='form:id']").val(#{p.notificationID});
$("[id='form:command']").click();
但是,这非常笨拙。 考虑寻找第三方JSF组件甚至实用程序库来实现该要求。 JSF实用程序库OmniFaces具有<o:commandScript>
组件。 另请参见其展示页面 。
<h:form>
<o:commandScript name="set0ToGrowlToShow" action="#{notificationBean.set0ToGrowlToShow}" />
</h:form>
同
set0ToGrowlToShow(#{p.notificationID});
(请注意,这是设置为HTTP请求参数,而不是作为操作方法参数)
JSF组件库PrimeFaces具有<p:remoteCommand>
,与<o:commandScript>
非常相似。 另请参见其展示页面 。 更重要的是,PrimeFaces有一个随时可用的<p:growl>
组件,它与你的jQuery插件基本相同! 另请参见其展示页面 。 你可以做的不是你的整个jQuery事情:
<p:growl globalOnly="true" autoUpdate="true" />
并通过消息提供它
facesContext.addMessage(null, message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.