最近在用jeesite开发东西,因为需要用到ajax进行数据的异步刷新。下面自己贴些代码留个笔记:
返回数据中不使用html拼接代码
在jsp页面中:
function huoqu(id) {
$('.alert_content').show();
$.ajax({
url: 'ykDisinfect/test1?id='+id,
type: 'GET',
async: true,
contentType: 'application/json;charset=utf-8' ,
dataType: 'json'
}).done(function(result) {
if (result.code == 200) {
$('#ykDisinfectType').val(result.obj.type);
$('#ykDisinfectWay').val(result.obj.way);
$('#ykDisinfectDate').val(result.obj.disinfectionDate);
$('#ykDisinfectIdOne').val(result.obj.id);
$('#ykDisinfectTheme').val(result.obj.theme);
$('#ykDisinfectRemark').val(result.obj.remark);
$('#ykDisinfectPerson').val(result.obj.disinfectionPerson);
$('.alert_content_top span').text('修改');
$('.input_date').text(result.obj.createTime);
$('.inPerson').text(result.obj.inPerson);
}
}).fail(function() {
console.log("error");
});
}
在后台的control中:
@RequiresPermissions("yk:disinfect:ykDisinfect:edit")
@RequestMapping(value = "/test1")
public @ResponseBody ResultData test1(@RequestParam(required=false) String id,Model model) {
try {
if (StringUtils.isBlank(id)) {
id = null;
} else {
id.trim();
}
YkDisinfect ykDisinfect=ykDisinfectService.get(id);
if(ykDisinfect == null) {
System.out.println("fail to connect!");
}
//model.addAttribute("page", page);
return this.success().setObj(ykDisinfect);
} catch(Exception e) {
dealException(e);
return fail();
}
}
需要拼接代码的:
jsp页面中的;
function queryDate() {
var beg=document.getElementById("begin").value;
var end=document.getElementById("end").value;
$.ajax({
url:'ykDisinfect/query?beg='+beg+'&end='+end,
type:'get',
async: true,
contentType: 'application/json;charset=utf-8' ,
dataType: 'json'
}).done(function (result){
if(result.code == 200) {
var list=result.list;
var ulObject = $(".disinfection_Type");
for(var i = 0; i < list.length; i++)
{
var temp = list[i];
var htm = '';
htm +='<tr>';
htm +='<td>'+temp.type+'</td>';
htm +='<td>'+temp.way+'</td>';
htm +='<td>'+temp.theme+'</td>';
htm +='<td>'+temp.remark+'</td>';
htm +='<td>'+temp.disinfectionDate+'</td>';
htm +='<td>'+temp.inPerson+'</td>';
htm +='<td>'+temp.createTime+'</td>';
htm +='<td>'+temp.disinfectionPerson+'</td>';
htm +='<shiro:hasPermission name="yk:disinfect:ykDisinfect:edit">';
htm +='<td οnclick="huoqu(\''+temp.id+'\')" style="cursor:pointer;">';
/* htm +='<td οnclick="huoqu()" style="cursor:pointer;">'; */
htm += '修改 </td>';
htm +='<td>';
htm +=' <a href="${ctx}/yk/disinfect/ykDisinfect/delete?id='+temp.id+'" οnclick="return confirmx('+'确认要删除该消毒信息吗?'+', this.href)">删除</a>';
htm +='</td> </shiro:hasPermission><tr>';
ulObject.after(htm);
}
}
}).fail(function(){
console.log("error");
});
}
在后台的control中:
@RequiresPermissions("yk:disinfect:ykDisinfect:view")
@RequestMapping(value = "/query")
@ResponseBody
public ResultData query(@RequestParam(required=false) String beg,String end,HttpServletRequest request) {
try {
Date ykBeg = null;
Date ykEnd = null;
Date ykDate = null;
if(beg.isEmpty() || end.isEmpty()) {
beg=null;
end=null;
} else {
ykBeg=DateUtils.format(beg, "yyyy-MM-dd"); //字符串转化成日期
ykEnd=DateUtils.format(end, "yyyy-MM-dd");
}
List<YkDisinfect> list1=new ArrayList<YkDisinfect>();
for(YkDisinfect ykd:ykDisinfectService.findList(new YkDisinfect())) {
ykDate=DateUtils.formatDateToFormat(ykd.getCreateTime(), "yyyy-MM-dd");
if(ykDate.getTime() >= ykBeg.getTime() && ykDate.getTime() <= ykEnd.getTime()) {
list1.add(ykd);
}
}
if(list1.isEmpty()) {
//return this.fail();
return this.fail();
}
return this.success().setList(list1);
}catch (Exception e){
dealException(e);
return fail();
}
}
在使用ajax的时候,自己一定要使用浏览器调试代码,吧ajax的url改正确,还有在传参的时候的格式