如何在JSF组件的on *属性中调用托管bean动作方法
我想在on*
属性中调用托管bean操作方法.在我的特殊情况下,如果用户闲置3分钟,则需要注销该用户,如下所示:
I'd like to invoke a managed bean action method in an on*
attribute. In my particular case I need to logout an user if the user is idle for 3 minutes as below:
<p:idleMonitor onidle="#{mybean.processTimeOut()}" timeout="180000" />
但是,托管的bean动作方法会在页面加载时立即被调用.这是怎么引起的,我该如何解决?
However, the managed bean action method is immediately invoked as the page loads. How is this caused and how can I solve it?
与所有JSF组件上的所有其他on*
属性一样, onidle
属性必须表示JavaScript回调,而不是JSF支持bean操作方法. on*
属性中的任何EL表达式在生成HTML输出时都将被立即评估为String
值表达式,以期望它们打印(部分)JavaScript代码.
Like as all other on*
attributes on all JSF components, the onidle
attribute must represent a JavaScript callback, not a JSF backing bean action method. Any EL expressions in on*
attributes would be evaluated immediately as String
value expressions during generating the HTML output in expectation that they print (part of) JavaScript code.
这就像您正在执行<h:outputText value="#{mybean.processTimeout()}">
一样.如果您删除了括号()
,则会遇到PropertyNotFoundException
,这也暗示了它被评估为值表达式而不是方法表达式.
It's exactly like as if you're doing <h:outputText value="#{mybean.processTimeout()}">
. If you had removed the parentheses ()
, you'd have faced a PropertyNotFoundException
which was also a hint at its own of it being evaluated as a value expression instead of a method expression.
为了使用JavaScript调用JSF支持bean方法,您需要一个附加的 <p:remoteCommand>
.
In order to invoke a JSF backing bean method using JavaScript, you need an additional <p:remoteCommand>
.
<p:idleMonitor onidle="processTimeout()" timeout="180000" />
<p:remoteCommand name="processTimeout" action="#{mybean.processTimeOut}" />
如果您不在PrimeFaces上,请转到此相关答案中发布的替代方法:
If you're not on PrimeFaces, head to the alternatives posted in this related answer: How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?