这么多天我试图完成这项任务,但是我失败了。 下面看一下我的jsp:
这里我有一张桌子,所有每一行都有刷新图标,我想在点击图标时刷新个别行。
onclick我正在调用一个函数 - > refreshRecord(value);
value(参数)是fileid(唯一) - >即我作为参数传递给函数的表的第一列。
ajax
正在使用checkStatusAndNumRecs
调用fileId
aciton,在操作类中我调用我的JPA
方法从数据库中获取数据,它正在读取行fileId
我已经通过并将其放入JSONObject
。
在ajax部分的jsp中,它执行suuccess一段时间并且错误执行大部分时间,
当它成功时,内部成功部分,它未能执行此行 - > var obj = jQuery.parseJSON(eval(data));
这意味着有时数据会返回到ajax,有时数据不会从动作类返回到ajax。
这是我的jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Upload</title>
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/style1.css" />
<link rel="stylesheet" type="text/css" href="css/button.css" />
<link href="css/common-style.css" rel="stylesheet" type="text/css" />
<style>
a{
color:white;
text-decoration:none;
}
</style>
<script type="text/javascript">
var id;
function refreshRecord(value)
{
id = value;
}
$(document).ready(function(){
$("#refresh").click(function(){
var fileId=id;
$.ajax({
type:"post",
url:"checkStatusAndNumRecs",
data:{fileId:fileId},
success:function(data)
{
alert(data);
var obj = jQuery.parseJSON(eval(data));->THis statement is not executing
aler("after JSON OBJECT"+obj.status);
$("#div1").html(obj.status);
$("#div2").html(obj.records);
},
error:function(data)
{
$("#div1").html("It was a failure !!!");
}
});
});
});
</script>
</head>
<body>
<%@include file="index1.html" %>
<div class="box2">
<div class="box3">
<s:property value="userId"/>
</div>
<center><h2>FILE STATUS</h2></center>
<center>
<form action="Upload" method="post" enctype="multipart/form-data">
<label for="myFile" class="text">Upload your file:</label>
<input type="hidden" name="upload" value="upload"/>
<input type="file" name="myFile" size="40" class="file"/>
<input type="submit" value="Upload" class="button"/>
<input type="submit" value="Refresh" class="button"/>
</form>
</center>
<center>
<s:if test="%{user.roles == 'admin'}">
<form action="manage" method="post" enctype="multipart/form-data">
<label for="myFile" class="text">Click to manage service providers:</label>
<input type="submit" value="Manage" class="button"/>
</form>
</s:if>
</center>
<center>
<table border="1" class="displaytab" id="rtable">
<s:if test="%{user.roles == 'admin'}">
<tr> <td colspan="10" style="background:#7395B8;color:white;font-size:18px;font-weight:bold;"><center>Admin</center></td></tr>
</s:if>
<tr>
<th>FileId</th><th>File Name</th><th>Upload Date</th><th>#Records</th><th>Status</th><th>Estimated Time</th><th>Processed Records</th><th>Generate Report</th><th></th><s:if test="%{user.roles == 'admin'}"><th>Controls</th></s:if>
</tr>
<s:iterator value="uploadList" var="m">
<tr>
<td><s:property value="%{#m.fileId}" /></td>
<td><s:property value="%{#m.fileName}" /></td>
<td><s:property value="%{#m.uploadDate}" /></td>
<td><div id="div2"><s:property value="%{#m.numRecords}" /></div></td>
<td><div id="div1"><s:property value="%{#m.status}" /></div></td>
<td>tbd</td>
<td><s:property value="%{#m.numRecords}" /></td>
<td><a href=""><img src="images/generate.png" title="Generate Report"></a></td>
<td><a href=""><img src="images/refresh.png" title="Refresh" id="refresh" onclick="refreshRecord(<s:property value="%{#m.fileId}" />);"></a></td>
<s:if test="%{user.roles == 'admin'}">
<td><a href=""><img src="images/details.png"></a>
<a href=""><img src="images/plus.png" title="Add Instance"></a>
<a href=""><img src="images/minus.png" title="Remove Instance"></a>
<a href=""><img src="images/download.png" title="Download"></a>
<a href=""><img src="images/reconnect.png" title="Reconnect"></a></td>
</s:if>
</tr>
</s:iterator>
</table>
</center>
<br>
<br>
<br>
<br>
<center>
<s:if test="%{user.roles == 'admin'}">
<!-- <select name="user names">
<s:iterator value="userNamesList">
<option value="emailColumn" > <s:property/>
</option>
</s:iterator>
</select> -->
<table border="1" class="displaytab" id="usertab">
<s:if test="%{uploadListMap.size() != 0}">
<tr> <td colspan="10" style="background:#7395B8;color:white;font-size:18px;font-weight:bold;">User Job Details</center></td></tr>
<tr>
<th>FileId</th><th>File Name</th><th>Upload Date</th><th>#Records</th><th>Status</th><th>Estimated Time</th><th>Processed Records</th><th>Generate Report</th><th></th><s:if test="%{user.roles == 'admin'}"><th>Controls</th></s:if>
</tr>
<s:iterator value="%{uploadListMap}">
<tr> <td colspan="10" style="background:#7395B8;color:white;font-size:18px;font-weight:bold;"><center><s:property value="key"/></center></td>
<s:iterator value="value" var="u">
<tr>
<td><s:property value="%{#u.fileId}" /></td>
<td><s:property value="%{#u.fileName}" /></td>
<td><s:property value="%{#u.uploadDate}" /></td>
<td><s:property value="%{#u.numRecords}" /></td>
<td><s:property value="%{#u.status}" /></td>
<td>tbd</td>
<td><s:property value="%{#m.numRecords}" /></td>
<td><a href=""><img src="images/generate.png" title="Generate Report"></a></td>
<td><a href=""><img src="images/refresh.png" title="Refresh" id="refresh" onclick="refreshRecord(<s:property value="%{#u.fileId}" />);"></a></td>
<td><a href=""><img src="images/details.png"></a>
<a href=""><img src="images/plus.png" title="Add Instance"></a>
<a href=""><img src="images/minus.png" title="Remove Instance"></a>
<a href=""><img src="images/download.png" title="Download"></a>
<a href=""><img src="images/reconnect.png" title="Reconnect"></a></td>
</tr>
</s:iterator>
</tr>
</s:iterator>
</s:if>
</table>
</s:if>
</center>
</div>
</body>
</html>
这是我的动作类:
P
ackage com.mxui;
import com.mxui.db.api.PersistenceService;
import com.mxui.db.service.*;
import org.json.simple.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
public class checkStatusAndNumRecsAction extends ActionSupport
{
/**
*
*/
private String status;
public String getStatus()
{
return status;
}
public void setStatus(String status)
{
this.status = status;
}
private long numRecords;
public long getNumRecords()
{
return numRecords;
}
public void setNumRecords(long numRecords)
{
this.numRecords= numRecords;
}
private String fileId;
public String getFileId()
{
return fileId;
}
public void setFileId(String fileId)
{
this.fileId = fileId;
}
public String execute()
{
JSONObject obj = new JSONObject();
System.out.println("here inside action-------------");
PersistenceService svc = PersistenceServiceImpl.getInstance();
status = svc.getStatusByFileId(fileId);
System.out.println("status is "+status);
numRecords = svc.getNumRecordsByFileId(fileId);
System.out.println("num records are "+numRecords);
obj.put("status", status);
obj.put("records", numRecords);
System.out.print("json data is "+obj);
return "SUCCESS";
}
}
请指导我,我在这么多天里为此奋斗,而且我也是ajax的新手。 谢谢你提前。
This is my struts.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.multipart.maxSize" value="1000000" />
<result-type name="json" class="org.apache.struts2.json.JSONResult" default="false" />
<package name="struts" extends="struts-default">
<action name="RegisterationProcess" class="com.mxui.RegisterationFormAction">
<result name="SUCCESS">registerationform.jsp</result>
<result name="customerRegister">successregisteration.jsp</result>
</action>
<action name="CheckUserValidation" class="com.mxui.CheckUserAction">
<result name="SUCCESS">noofrows.jsp</result>
</action>
<action name="ProcessLogin" class="com.mxui.LoginAction">
<result name="LOGIN" >login.jsp</result>
<result name="REGISTER">registerationform.jsp</result>
<result name="ERROR" type="redirect">login.jsp</result>
<result name="FILEUPLOAD" type="redirect">Upload</result>
</action>
<action name="Upload" class="com.mxui.UploadFileAction">
<result name="SUCCESS">fileupload.jsp</result>
<result name="LOGINERROR" type="redirect">ProcessLogin</result>
<result name="PREVIEW" type="redirect">FilePreviewAction</result>
<result name="ERROR">error.jsp</result>
</action>
<action name="FilePreviewAction" class="com.mxui.FilePreviewAction">
<result name="SUCCESS">filepreview.jsp</result>
<result name="JOBCREATED" type="redirect">Upload</result>
<result name="ERROR" type="redirect">ProcessLogin</result>
</action>
<action name="ServiceProviderProcess" class="com.mxui.ServiceProviderAction">
<result name="SUCCESS">CreateProvider.jsp</result>
<result name="serviceprovider" type="redirect">Upload</result>
</action>
<action name="UpdateServiceProviderProcess" class="com.mxui.UpdateServiceProviderAction">
<result name="SUCCESS">updateserviceprovider.jsp</result>
<result name="updated" type="redirect">Upload</result>
</action>
<action name="manage" class="com.mxui.ManageServiceProviderAction">
<result name="SUCCESS">manageserviceprovider.jsp</result>
</action>
</package>
<package extends="struts-default,json-default" name="name" namespace="">
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
<action name="checkStatusAndNumRecs" class="com.mxui.checkStatusAndNumRecsAction" method="execute">
<result type="json"></result>
</action>
</package>
</struts>
答案 0 :(得分:0)
在JSP页面
$.ajax({
type: 'POST',
url: 'checkStatusAndNumRecs',
data:{fileId:fileId},
dataType: 'json',
async: false ,
contentType: 'application/json; charset=utf-8',
success: function(data){
alert(data);
var obj = jQuery.parseJSON(eval(data));->THis statement is not executing
alert("after JSON OBJECT"+obj.status);
$("#div1").html(obj.status);
$("#div2").html(obj.records);
},
error:function(data)
{
$("#div1").html("It was a failure !!!");
}
});
});
});
<强> struts.xml中强>
将全局结果定义为
<result-type name="json" class="org.apache.struts2.json.JSONResult" default="false" />
对于行动
<package extends="struts-default,json-default" name="name" namespace="">
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
<action name="YouAction" class="YourActionClass" method="executeMethod">
<result type="json"></result>
</action>
</package>