5

In the Primefaces User Guide it shows examples of how to make AJAX calls to the server

PrimeFaces.ajax.AjaxRequest('/myapp/createUser.jsf',
{
    formId: 'userForm',
    oncomplete: function(xhr, status) {alert('Done');}
});

What I can't figure out is how to call a particular method. My goal is to invalidate the session from the client using JavaScript.

3 Answers 3

18

RemoteCommand is a nice way to achieve that because it provides you a JavaScript function that does the stuff (calling backing bean, refreshing, submitting a form etc., everything that command link can do).

From PrimeFaces 3.4 documentation:

<p:remoteCommand name="increment" actionListener="#{counter.increment}"
out="count" />

<script type="text/javascript">
function customFunction() {
    //your custom code
    increment(); //makes a remote call
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

4

What I've typically done is put a hidden p:commandLink on the page, then have Javascript call the click() event on it.

<p:commandLink id="hiddenLink" 
   actionListener="#{bean.methodToInvoke}" style="display:none"/>

Then

$('#hiddenLink').click();

3 Comments

Great idea. But how to pass parameters to the bean?
Depends on whether those parameters are fixed or need to be adjusted client-side via javascript as well. If they don't change, you can use f:attribute. If they might change client-side, I've used h:inputHidden elsewhere on the form to push those along to the managed bean.
In my case, the element from <p:commandLink id="hiddenLink"> is NOT given the id "hiddenLink". Instead, it is given the id "j_idt5:hiddenLink". Therefore, I cannot find it.
1

Do it in the @PostConstruct method of the request scoped bean which is associated with the requsted JSF page by EL like #{bean}.

@ManagedBean
@RequestScoped
public class Bean {

    @PostConstruct
    public void init() {
        // Here.
    }

}

Unrelated to the question, I only wonder why you would ever do it that way? JSF/PrimeFaces offers much nicer ways using <f:ajax> and <p:ajax> and consorts.

Is it the intent to run this during Window's unload or beforeunload events? If so, then I have to warn you that this is not reliable. It's dependent on the browser whether such a request will actually reach the server or not. More than often it won't. Use it for pure statistical or premature cleanup purposes only, not for sensitive business purposes.

5 Comments

I can have, at any given time, up to 5 running applications, each with its own session. As the user switches from App to App, I keep the other applications "alive" so they don't time out from neglect. However, once the "active" App times out, then I need to "logout" the others to make sure they don't stay around. I have 1 servlet App, 3 struts Apps and 1 JSF App. Any one could be the "active" App at any point in time.
If they runs at the same container, just let them all share the same session. In for example Tomcat, you can achieve this by emptySessionPath="true" in <Connector> in /conf/server.xml.
Thanks, that is what I suggested, but these are existing apps running on Weblogic. I don't think they can guarantee that the data from one app won't collide with the others. In other words, I don't think they can share a single session with out trampling each other.
Well, much luck with that. I would anyway reconsider a share session or at least SSO.
Currently, we have a home-grown SSO, so that, from the JSF "Portal" we can SSO into each app. The problem is keeping them alive and then in the end logging them all out. I suspect that I can log them out via the SSO process once I have logged them in. Assuming the JSF app hasn't timed out. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.