Ankur Chachra
Ankur Chachra

Reputation: 131

Auto-refresh a page/div in Struts2

I have a Struts2 Web Application in which my home page displays data in a table that was fetched from a Java class. The Java class, in turn, fetches the data from my database. Now, in case of new records being entered into the database after the page has been loaded, I have to manually refresh the page to view them in the table. Is there any way I can get the page/div to auto refresh?

Here's my table code

<display:table id="reqtablenew" name="requestlist" requestURI="" pagesize="6" class = "newreqtable">

    <display:column title="Select" >
    <input type="radio" name="reqradio" />
    </display:column>                
    <display:column title="ID" property="requestid"></display:column>
    <display:column title="Requestor" property="requestor"></display:column>
    <display:column  title="Approver" property="approver"></display:column>
    <display:column  title="Status" property="status"></display:column>
    <display:column  title="Product" property="product"></display:column>
    <display:column  title="Version" property="version"></display:column>
    <display:column  title="Source Path" property="source"></display:column>
    <display:column  title="Destination Path" property="destination"></display:column>

</display:table>

A brief introduction to the code,

requestlist - ArrayList in the Java class that stores the objects from database properties : Individual members of the ArrayList object

I am not using Session. Any thoughts as to how I might accomplish this task would be very helpful. I have used Ajax in the rest of my application but I can't figure out how to use it here.

Upvotes: 0

Views: 3432

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

Create an action that just returns this table.

Make periodic ajax calls to this action & get the response & embed in your DOM.

If you've used ajax in other parts of your application, it's just the same.

Action

class MyAction{
public String execute(){
//create all lists required for table
return SUCCESS;
}
}

struts.xml

<action name="tableaction" class="actions.MyAction">
<result>/table.jsp</result>
</action>

Table.jsp

//the display tag code

Javascript code :

function ajaxCall(){
      $.ajax({
          url:"tableaction",
          success:function(data){
          $("targetdiv").html(data);
          }
      });
}
setInterval(ajaxCall(),3000);

All above code is just a sample UNTESTED code to give an idea on how to implement it.

Upvotes: 1

Related Questions