如何在 JSF 组件的 on* 属性中调用托管 bean 操作方法 [英] How to invoke a managed bean action method in on* attribute of a JSF component
问题描述
我想在 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 方法,您需要额外的
.
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 上,请前往此相关答案中发布的替代方案:如何使用原生 JavaScript 在 HTML DOM 事件上调用 JSF 托管 bean?
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?
这篇关于如何在 JSF 组件的 on* 属性中调用托管 bean 操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!