超链接删除

来源:互联网 发布:搜索一下淘宝网 编辑:程序博客网 时间:2024/05/22 05:13

页面:

<script type="text/javascript"> 

//删除照片
function delParkingPhoto(id){
$.ajax({
url:"../../app/parkingphoto/parking-photo-del?id="+id,
type:"POST",
});
//删除以后重新刷新页面
location.reload(true);
     } 


 </script>

 <a href="JavaScript:delParkingPhoto('${pp.id}')" onclick="return confirm('确定删除此照片')" rel="external">删除</a>



后台代码:

@Path("parking-photo-del")
@POST
@Produces({ MediaType.APPLICATION_JSON })
public String delPhoto(@QueryParam("id") String id) throws IOException{
String msg="";
try {
if(id!=null || id!=""){
ParkingPhoto pp=jpaTemplate.find(ParkingPhoto.class, id);
//删除照片表中的记录
TransactionTemplate tt=app.getTransactionTemplate();
tt.execute(new TransactionCallback<Object>(){
String id;
public TransactionCallback<?> setId(String id){
this.id = id;
return this;
}


@Override
public Object doInTransaction(TransactionStatus status) {
try{
ParkingPhoto pp=jpaTemplate.find(ParkingPhoto.class, id);
   jpaTemplate.remove(pp);
}catch(Exception e){
System.out.println("未找到照片信息");
}
return null;
}
}.setId(id));
//删除文件夹下面的照片
RequestContext rctx=RequestContext.get();
String realPath = rctx.getServletContext().getRealPath(pp.getPath());
File file = new File(realPath);
if(file.exists() && file.isFile())
file.delete();
msg="删除成功!";
System.out.println("删除成功");
}
} catch (Exception e) {
e.printStackTrace();
msg="对不起,删除失败!";
System.out.println("删除失败");
}
return msg;
}