SpringMVC的删除功能

Dao层

package net.roseindia.dao;

import java.util.Date;
import java.util.List;

import net.roseindia.model.Article;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao {

  @Autowired
  private SessionFactory sessionFactory;

  // To Save the article detail
  public void saveArticle(Article article) {
    article.setAddedDate(new Date());
    sessionFactory.getCurrentSession().saveOrUpdate(article);
  }
  public void deleteArticle(Integer articleId){
      System.out.println("Dao-->"+articleId);
      Session session=sessionFactory.openSession();
      Article article = (Article) session.get( Article.class,articleId);
      session.beginTransaction();
      if (null != article) {
          session.delete(article);
      }
      session.getTransaction().commit();
      session.close();

  }
  
  // To get list of all articles
  @SuppressWarnings("unchecked")
  public List<Article> listArticles() {    
    return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
  }
} 

Jsp页面如下

 

<%@  taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 
<html>

<head>

<title>All Articles</title>

</head>

<body>

<h1>List Articles</h1>

<a href="articles/add.html">Add Article</a>
 
<c:if test="${!empty articles}">
 
<table>
 
<tr>
 
<th>Article ID</th>
 
<th>Article Name</th>
 
<th>Article Desc</th>
 
<th>Added Date</th>
 
</tr>

<c:forEach items="${articles}" var="article">
 
<tr>
 
<td><c:out value="${article.articleId}"/></td>
 
<td><c:out value="${article.articleName}"/></td>
 
<td><c:out value="${article.articleDesc}"/></td>
 
<td><c:out value="${article.addedDate}"/></td>
 
<!-- <td><a href="#" onclick="getData('articles.do?actionMethod=delete&queryId=${article.articleId}','','workspace');">delete</a></td>  -->
 
<!-- <td><a href="/articles/delete/${article.articleId}">delete</a></td>  -->
<td><a href="articles/delete.do?ID=${article.articleId}">delete</a></td> 

</tr>
 
</c:forEach>
 
</table>

</c:if>
 
</body>
</html>

 

posted @ 2013-11-28 15:11  Kstyjobx!!  阅读(4185)  评论(0编辑  收藏  举报