How to invoke a managed bean action method in on* attribute of a JSF component
Asked Answered
G

1

2

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" /> 

However, the managed bean action method is immediately invoked as the page loads. How is this caused and how can I solve it?

Gyno answered 17/3, 2015 at 10:23 Comment(2)
What do you want to achieve? Redirect user to a specific page after 3 minutes? Invalide his session and show a custom message? "logs out the user immediately lands in the main page" is not very clear to me, could you clarify?Hoahoactzin
@Mathieu Castets yes I want to redirect the user to login page if he is idle for 3 minutes. The code have provided its in mainDashboard but it logs out the user to login page immedialy it initialize to mainDashboardGyno
A
3

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.

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.

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}" />

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?

Ammonal answered 17/3, 2015 at 10:56 Comment(2)
@BalusCafter trying your example this is the "error am getting XML Parsing Error: no element found"Gyno
I have seen my problem I had not included <h:form> between the two tags thanks alotGyno

© 2022 - 2024 — McMap. All rights reserved.