花几天学个东西,还是觉得很不错...
首先要弄好框架之前的准备,jar包的准备...下面提供下载...
(1)把目录全部建好...
net.springmvc.model
net.springmvc.dao
net.springmvc.servlet
net.springmvc.controller
(2)首先配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sys/springmvc/action-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/forms/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(3)model
package net.spring.mvc.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "article")
public class Article {
@Id
@GeneratedValue
@Column(name = "article_id")
private Long articleId;
@Column(name = "article_name", nullable = false, length=20)
private String articleName;
@Column(name = "article_desc", nullable = false)
private String articleDesc;
@Column(name = "date_added")
private Date addedDate;
public Article() {
}
public Long getArticleId() {
return articleId;
}
public void setArticleId(Long articleId) {
this.articleId = articleId;
}
public String getArticleName() {
return articleName;
}
public void setArticleName(String articleName) {
this.articleName = articleName;
}
public String getArticleDesc() {
return articleDesc;
}
public void setArticleDesc(String articleDesc) {
this.articleDesc = articleDesc;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
}
(4)dao
public interface ArticleDao {
// To Save the article detail
public void saveArticle ( Article Article );
// To get list of all articles
public List<Article> listArticles();
}
@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);
}
// To get list of all articles
@SuppressWarnings("unchecked")
public List<Article> listArticles() {
return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
}
}
(5)service
public interface ArticleService {
public void addArticle(Article article);
public List<Article> listArticles();
}
@Service("articleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleDao articleDao;
public ArticleServiceImpl() {
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticle(Article article) {
articleDao.saveArticle(article);
}
public List<Article> listArticles() {
return articleDao.listArticles();
}
}
(6)controller
@Controller
@RequestMapping("/articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute(" article") Article article,
BindingResult result) {
articleService.addArticle( article);
return new ModelAndView("redirect:/articles.html");
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView listArticles() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("articles", articleService.listArticles());
return new ModelAndView("articlesList", model);
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");
}
}
(7)addArticle.jsp and articleList.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head><title>Add Article</title></head>
<body>
<h1>Add Article</h1>
<c:url var="viewArticlesUrl" value="/articles.html" />
<a href="${viewArticlesUrl}">Show All Articles</a>
<br />
<br />
<c:url var="saveArticleUrl" value="/articles/save.html" />
<form:form modelAttribute="article" method="POST" action="${saveArticleUrl}">
<form:label path="articleName">Article Name:</form:label>
<form:input path="articleName" />
<br />
<form:label path="articleDesc">Article Desc:</form:label>
<form:textarea path="articleDesc" />
<br />
<input type="submit" value="Save Article" />
</form:form>
</body>
</html>
<%@ 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>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
搞定~~测试通过...
接下来把DAO封装成通用的~~~