我正在使用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>发布于 2013-03-28 04:06:03
您当前的尝试只是将给定的EL表达式解释为值表达式,并在生成嵌入了JS代码的HTML输出时立即打印其结果。就像你在使用<h:outputText>一样。这确实是行不通的。
然而,功能需求是可以理解的。标准的JSF API没有为此提供现成的解决方案。如果您想坚持使用标准JSF,最好的办法是创建一个具有隐藏命令链接的隐藏表单,您可以使用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具有用于此目的的组件。另请参阅其showcase page。
<h:form>
<o:commandScript name="set0ToGrowlToShow" action="#{notificationBean.set0ToGrowlToShow}" />
</h:form>使用
set0ToGrowlToShow(#{p.notificationID});(请注意,这被设置为HTTP请求参数,而不是操作方法参数)
JSF组件库PrimeFaces具有与<o:commandScript>非常相似的。另请参阅其showcase page。更重要的是,PrimeFaces有一个现成的组件,它的功能与您的jQuery插件基本相同!另请参阅其showcase page。你可以这样做,而不是整个jQuery的事情:
<p:growl globalOnly="true" autoUpdate="true" />并通过以下方式向其提供消息
facesContext.addMessage(null, message);另请参阅:
发布于 2013-03-28 03:59:05
您需要了解,您需要调用在服务器中运行的java方法,并且您不能直接调用它。
在您的情况下,我建议使用AJAX或在页面加载时读取并使用它(如果对您可行)
查看如何使用AJAX with Jquery
https://stackoverflow.com/questions/15668132
复制相似问题