以我的对SSH框架整合jquery-ajax的理解:在jsp页面中调用$.ajax方法,然后到action里面,我使用Printwriter把json格式的对象print,之后就在ajax的回调函数中调用$.each进行数据的显示。 数据的显示功能已经实现了,然后我就开始做数据增加功能,一如既往:显示在jsp界面用$.ajax()方法,把信息提交到action里面,并且操作成功,我在action里的checkin方法里面并没有用printwriter传送任何值,最后我在jsp的回调函数里面把表格数据清空,然后加上<th,最后又把显示数据的$.each代码给复制过来,我只是想测试一下表单清空和数据增加成功没,怪异的是它已经能实现数据增加并且table异步刷新了,我很疑惑第二个$.each中的date是哪里来的,是第一次的数据没清空?容我查查资料,思考思考!!
代码贴一下:
<script>
$(document).ready(function(){
alert("dom加载完毕");
$.ajax({ url: './ajax/getallcheck.action',
type: 'get',
dataType: 'json',
success: function(data) {
alert("第一个毁掉函数执行");
$.each(data, function(index, item){
var daystr=item.day;
var dayarr=daystr.split('-');
var day=dayarr[0]+"-"+dayarr[1]+"-"+dayarr[2];
var checkinstr=item.checkin;
var checkinarr=checkinstr.split('-');
var checkin=checkinarr[3]+":"+checkinarr[4]+":"+checkinarr[5];
var checkoutstr=item.checkout;
var checkoutarr=checkoutstr.split('-');
var checkout=checkoutarr[3]+":"+checkoutarr[4]+":"+checkoutarr[5];
$("#checklist").append("<tr id='check" + item.checkid + "'>"+
"<td>" + item.checkid + "</td>"+
"<td>" + item.staff.name + "</td>"+
"<td>" + item.sid + "</td>"+
"<td>" + checkin + "</td>"+
"<td>" + checkout + "</td>"+
"<td>" + item.checked + "</td>"+
"<td>" + item.late + "</td>"+
"<td>" + item.leave + "</td>"+
"<td>" + day + "</td>"+
"<td>" + item.about + "</td>"+
"</tr>");
});
}});
//checkin
$("#checkin").click(function(){
var checkinid=$("#checkin_id").val();
$.ajax({
url: './ajax/checkin.action',
type: 'post',
dataType: 'json',
data: {'checkinid':checkinid},
success: function(data) {
alert("毁掉函数返回");
//$("#checklist").children().remove();
$("#checklist").children().remove();
//重新查询数据库
$("#checklist").append("<tr>" +
"<th>考勤编号</th>" +
"<th>员工姓名</th>" +
"<th>员工编号</th>" +
"<th>上班刷卡时间</th>" +
"<th>下班刷卡时间</th>" +
"<th>出勤成功</th>" +
"<th>迟到</th>" +
"<th>请假</th>" +
"<th>日期</th>" +
"<th>备注</th>" +
"</tr>");
$.each(data, function(index, item){
var daystr=item.day;
var dayarr=daystr.split('-');
var day=dayarr[0]+"-"+dayarr[1]+"-"+dayarr[2];
var checkinstr=item.checkin;
var checkinarr=checkinstr.split('-');
var checkin=checkinarr[3]+":"+checkinarr[4]+":"+checkinarr[5];
var checkoutstr=item.checkout;
var checkoutarr=checkoutstr.split('-');
var checkout=checkoutarr[3]+":"+checkoutarr[4]+":"+checkoutarr[5];
$("#checklist").append("<tr id='check" + item.checkid + "'>"+
"<td>" + item.checkid + "</td>"+
"<td>" + item.staff.name + "</td>"+
"<td>" + item.sid + "</td>"+
"<td>" + checkin + "</td>"+
"<td>" + checkout + "</td>"+
"<td>" + item.checked + "</td>"+
"<td>" + item.late + "</td>"+
"<td>" + item.leave + "</td>"+
"<td>" + day + "</td>"+
"<td>" + item.about + "</td>"+
"</tr>");
});
}});
});
//checkin------- end
});
</script>
Action里面
public String getallcheck()
{ HttpServletResponse res = ServletActionContext.getResponse();
res.setCharacterEncoding("utf-8");
res.setCharacterEncoding("utf-8");
res.setContentType("text/html");
PrintWriter out = null;
try {
out = res.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
List list=checkService.getallcheck();
System.out.println("list在这里得到了hibernate的值");
//下面三局在这里不会空指针,但在checkin()里会空指针
/* Staff checkStaff=new Staff();
checkStaff=(Staff)staffService.getOneStaff(1);
System.out.println("-------checkstaff:"+checkStaff.getSid());
*/
//过滤外键
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
JSONArray jsonArray=JSONArray.fromObject(list,jsonConfig);
System.out.println(jsonArray.toString());
out.print(jsonArray);
out.flush();
out.close();
return SUCCESS;
}
public String checkin()
{
System.out.println("chectionAction----------checkinid:"+checkinid);
Check addcheck=new Check();
addcheck.setSid(checkinid);
Date time=new Date();
addcheck.setDay(time);
addcheck.setCheckin(time);
Staff checkStaff=new Staff();
checkStaff=(Staff)this.staffService.getOneStaff(checkinid);
addcheck.setStaff(checkStaff);
System.out.println("-------checkstaff:"+checkStaff.getSid());
System.out.println("-------checkstaff:"+checkStaff.getName());
boolean checkok=checkService.checkin(addcheck);
if(checkok==true)
return SUCCESS;
else return ERROR;
}