【发布时间】:2015-01-04 09:14:30
【问题描述】:
我正在尝试从我的图库中删除图像而不刷新页面。解决方案是使用 Ajax,但我不知道如何,因为我是 ajax 的新手。这是用于从我的数据库中删除图像的代码,这是在我的 DAOImpl 中实现的:
@Override
public void eliminaImage(int id_foto) {
Image imageDaEliminare;
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Image.class);
crit.add(Restrictions.eq("id_foto", id_foto));
imageDaEliminare = (Image) crit.uniqueResult();
Session session = sessionFactory.getCurrentSession();
session.delete(imageDaEliminare);
session.flush();
}
这是我的控制器,用于显示画廊并从中删除图像:
@RequestMapping(value = "/homeUtente", method = RequestMethod.GET)
public ModelAndView getIndex(){
ModelAndView mav = new ModelAndView();
mav.addObject("galleria", usersService.getAllFoto());
return mav;
}
@RequestMapping(value="/homeUtente", method = RequestMethod.POST)
public ModelAndView eliminaImage(HttpServletRequest request,@RequestParam("id_foto") int id_foto) {
usersService.eliminaImage(id_foto);
ModelAndView mav = new ModelAndView();
mav.addObject("galleria", usersService.getAllFoto());
return mav;
}
这是我的查看页面:
<c:forEach var="gallery" items="${galleria}">
<div class="uk-overlay">
<a href="data:image/jpeg;base64,${gallery.value}" data-lightbox="roadtrip">
<img id="my_image2" src="data:image/jpeg;base64,${gallery.value}" width="400" height="250" alt=""></img>
</a>
<div class="elimina">
<form action="homeUtente" method="post">
<input type="hidden" name="id_foto" value="${gallery.key}">
<button class="uk-icon-button uk-icon-trash-o" id="submit"></button>
</form>
</div>
</div>
</c:forEach>
最后我尝试为删除图像编写此 ajax 代码,但我不知道如何隐藏已删除的图像:
$('.elimina').each(function(){
$.ajax({
url:"homeUtente",
type:'post',
data:{id:'this.id_foto'},
success:function(result){
//TO IMPLEMENT!
}
error:function(err){
}
}));
【问题讨论】:
标签: java jquery ajax hibernate spring-mvc