如何使用 Struts2 从 Apache Tomcat 服务器动态更新 div 文本内容
How to update div text content dynamically from Apache Tomcat server using Struts2
我正在开发一个应用程序,用户可以在其中将文件上传到服务器,然后服务器处理文本文件。处理时,我需要用服务器发送的一些文本更新用户。
有什么方法可以动态更新 div 的文本内容而无需重新加载页面
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> </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>
行动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
方法将一些数据写入数据库。我需要每 2 秒读取一次数据库并在 longRunningAction-wait.jsp
中更新状态 div
您可以使用 sj:div
生成 HTML <div/>
并使用 Ajax 调用加载其内容。
示例:
将内容加载到 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>
我正在开发一个应用程序,用户可以在其中将文件上传到服务器,然后服务器处理文本文件。处理时,我需要用服务器发送的一些文本更新用户。
有什么方法可以动态更新 div 的文本内容而无需重新加载页面
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> </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>
行动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
方法将一些数据写入数据库。我需要每 2 秒读取一次数据库并在 longRunningAction-wait.jsp
您可以使用 sj:div
生成 HTML <div/>
并使用 Ajax 调用加载其内容。
示例:
将内容加载到 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>