0

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.

Shruti
  • 97
  • 1
  • 4
  • 14

1 Answers1

1

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!

Jarob22
  • 370
  • 1
  • 6
  • 13
  • You seem to have given a good solution however, I am not familiar with AJAX. So i dont really understant what is happening in this code. Where am i supposed to use this code? And can you please edit the code with comments so that i know what the statements are actually doing. You said "This will call whatever url you want to call JAVA side" , however i really want to just invoke a method. I Hope you can make sense of my confusion. – Shruti Apr 07 '14 at 14:32
  • Okay, so are you using any sort of web framework Java side, like Spring? – Jarob22 Apr 07 '14 at 14:35
  • I am using JSF2.2 . I have to let you know that i m still a rookie, this is my first major project and i m learning the applications of XHTML, CSS, JavaScript, Jquery and Java alongside. So, maybe you are using advanced concepts which i have not come across yet. Actually this "button" that i am talking about is like a menu on my webpage. Menus are mostly anchor tags. However as an anchor tag cannot invoke a java method, i used a button. – Shruti Apr 07 '14 at 14:41
  • What i am actually trying to do is that in my application, i have a functionality called "View My Tasks". By Clicking on it, the user that has logged in can straightaway view his tasks. The tasks are being fetched from the database. So i dont want the user to enter his id to fetch the tasks. The id is saved while the session started. – Shruti Apr 07 '14 at 14:44
  • Ok, and I assume the action="#{empDutySchedBean.viewMyTask}" makes it execute your viewMyTask method in your java? – Jarob22 Apr 07 '14 at 15:40
  • Take a look at this: http://stackoverflow.com/questions/6175722/redirect-from-servlet which should help. If you were using Spring instead, I'd be able to help you more, but I don't really know anything about JSF. If that link doesn't help you, google for how to redirect with JSF specifically. – Jarob22 Apr 07 '14 at 17:09