RaceBase
RaceBase

Reputation: 18848

Refresh Page using Ajax

I have a scenario where I need to call a Java method which sets some request properties and the same has to be reflected in the jsp page.

<script type="text/javascript">
  setInterval(function() {
    ajaxAction($('#refresh'), 'moveETHAction_fetchExecutorData');
    return false;
    //callNextMethod(document.getElementById('moveForm'), 'moveETHAction_',
        //  'fetchExecutorData');
  }, 60 * 1000);

      function ajaxAction(form, url) {
    /* the time and hour are added to the request for create an unique URL for IE.
     * The call to the server is realised only if the url is different...
     */
    params = Form.serialize(form) + '&ms=' + new Date().getTime();
    currentFocused = document.activeElement.id;
    lastActionCalled = url;
    formUsed = form;
    var myAjax = new Ajax.Request(url, {
        method : 'post',
        parameters : params,
        onComplete : showResponse
    });
}
function showResponse(originalRequest) {
    xml = originalRequest.responseXML;
}

 </script>

as you see, I want to hit action class and the respective method. However I don't have to replace anything in view part except fetching some request attributes which will be set in Java action class.

the total view part is in one form ('sendOrder') and i included one more form('refresh' within the main form. sorry, i don't know how to achieve my requirement. I just gave this try.

Please let me know how can I resolve this? It's not mandatory to use AJax, all i need to do is hit the java method and display the same page with updated request properties.

Upvotes: 0

Views: 1537

Answers (1)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

I am not sure how many fields you want to get from the Action and you can very well use Jquery along with Struts2-Json Plugin to do the work for you.

S2 Json plugin will help you to send back response in JSON format and you can use Jquery JSon feature to parse those fields and fill the values as per your way.

This is what you have to do

var myFormValues='id='+uuid;
    $.getJSON('myAction',myFormValues,function(data) {
     $.each(data.myObject,function(key, value){
         alert(key);
                 alert(value);
    });
     });

here key is the name of the field and value is the value it hold with respect to that fiel

In above you are calling a action namely myAction which will return the JSON back based on the value sent by you myFormValues. This is a mock for your action

public class MyAction extends ActionSupport{
   private String id;
   //getter and setter
   private String vallue1;
   private String vallue2;
   private String vallue3;
   //there getter ans setters

   public String execute() throw Exception{
       //Fill you values based on the condition
       return SUCCESS;
   }
}
<package name="JSON" extends="json-default">
   <action name="myAction"  class="MyAction">
     <result type="json"></result>
 </action>

Hope this will give you some idea

Upvotes: 1

Related Questions