With the click of an HTML button, how to INVOKE a java method AND REDIRECT to another HTML page?

StackOverflow https://stackoverflow.com/questions/22911512

Question

I have a button through which i INVOKE a java method :

<h:commandButton value="My Schedule" action="#{empDutySchedBean.viewMyTask}" rendered="#{welcomeBean.workSchedule}"   class="mainLinks"/>

now through this method viewMyTask() i get a List of values returned from the database.

 public List viewMyTask()
{        
    setEmpID((String) session.getAttribute("userName"));

   setEmpDuty(ts.getMyTask(getEmpID()));


    return empDuty;
}

From here i want tht with the click of a button, the user is redirected to another page and the List data shows up. I dont understand how to REDIRECT to another HTML page and display the LIST data.

NOTE: --> this button is basically a menu on my webpage. For the other menus i have used an anchor tag. However as you cannot invoke a method using anchor tag, i have used button tag. But because of this, I am not being able to redirect

Some Ways that i think it can be solved are:

  1. use anchor tag instead on button to redirect to the other page AND then use onLoad method of javascript to INVOKE java method
  2. invoke another method after viewMyTask() to perform the redirection.

    I dont kw how these methods would work though.

Était-ce utile?

La solution

First off, I assume you already know how to return the data Java side, in which case this is really just a javascript question. To do simple post-gets, I usually use Ajax, like so:

new Ajax.Request(myUrlToCall, {
method:'POST',
    onComplete: function(transport) {
        window.location.replace(theRedirectUrl);
    }
});

This will call whatever url you wanted to call Java side, then on the completion of this call, will redirect you to whatever url you want to go to. If you need to send some parameters to the java side, all you need to add is:

parameters:myParamVar,

after the method:'POST' line, and if you want your java side to return the url, then all you'd need to do is send it as json or similar in the response, then do:

window.location.replace(transport.responseJSON["theRedirectUrl"])

Hope that helped!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top