Marimuthu Mahalingam
Marimuthu Mahalingam

Reputation: 43

How to update div text content dynamically from Apache Tomcat server using Struts2

I'm developing an application where user uploads a file to the server and server process the text file. While processing, I need to update the user with some text sent from the server.

Is there any way through which I can update text content of a div dynamically without reloading the page

fpoAnalysis.jsp:

 <form action="Process" method="post" enctype="multipart/form-data" onsubmit="return validate()">
        <table align="center" style="width: 90%; margin: auto; outline: buttonshadow;">
            <tr>
                <td><label for="myFile" style="font: oblique; cue-after: inherit;">Upload All User Attributes File....</label></td>
                <h4 align="center" style="color: red;"> <s:actionerror /> </h4>
                <td><input type="file" name="file" required="true" accept=".csv" /></td>
            </tr>
            <tr>
                <td></td>
                <td><s:textfield id="month" name="month" type="number"label="Select Month:"></s:textfield></td>
                <td><s:textfield id="year" name="year" type="number" label="Select Year:"></s:textfield></td>
                <td>&nbsp</td>
        </table>
            <div id="sub" align="center">
                <s:submit type="submit" value="Process" align="center" />
            </div>  
    </form>

longRunningAction-wait.jsp:

<div class="container">
  <h2 align="center">Please Wait while we process your Data...</h2>
  <div class="progress" align="center">
    <div align="center" class="progress-bar progress-bar-striped active role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:40%">
      40%
    </div>
  </div>
  <div class="status">Status comes here!</div>
</div>

action class:

 public String execute(){
    destPath=request.getServletContext().getRealPath("")+"\\WEBINF\\Data";
            try{
                File destFile  = new File(destPath, fileFileName);
                FileUtils.copyFile(file,destFile);
                appPath=request.getServletContext().getRealPath("")+"\\WEB-INF\\Data";
                t=utils.LogUtils.generateData(4,2014,appPath,destFile.getAbsolutePath(),
                                                                                appPath);
            }catch(Exception ex){
                addActionMessage("User Attributes file is not valid!!");
                return "input";
            }
            return "success";
        }

struts.xml:

<action name="Process" class="fpoActions.Process" method="execute">
    <interceptor-ref name="defaultStack">
        <param name="fileUpload.maximumSize">150971520</param>
    </interceptor-ref>
    <interceptor-ref name="completeStack" />
    <interceptor-ref name="execAndWait" />
    <exception-mapping exception="java.lang.Exception" result="exception" />
    <result name="exception">exception.html</result>
    <result name="wait">WEB-INF/fpo/longRunningAction-wait.jsp</result>
    <result name="success">WEB-INF/fpo/DownloadFpo.jsp</result>
    <result name="input">WEB-INF/fpo/fpoAnalysis.jsp</result>
</action>

LogUtils.generateData method writes some data to a database. I need to read the database every 2 secs and update the status div in longRunningAction-wait.jsp

Upvotes: 1

Views: 651

Answers (1)

Roman C
Roman C

Reputation: 1

You can use <sj:div> which generates a HTML <div/> and loads its content using Ajax call.

Example:

Load content into div:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
  <head>
    <sj:head/>
  </head>
  <body>
    <div id="div1">Div 1</div>
    <s:url var="ajaxTest" value="/AjaxTest.action"/>
    <sj:div href="%{ajaxTest}">Initial Content</sj:div>
  </body>
</html>

Upvotes: 2

Related Questions