ArticleService.java
This is the interface which declares methods which will be used in controller class.
package net.roseindia.service; |
ArticleServiceImpl.java
This is the implementation class of ArticleService Interface. It defines all the methods declared in ArticleService interface. These methods uses Dao classes to interact with the database.
@Service("articleService") annotation is used to declare it as service bean and its name articleService will be used to auto wire its instance in controller class.
@Transactional annotation is used to declare the method transactional. You can also use this at the class level to declare all methods transactional.
package net.roseindia.service; |
ArticleController.java
This is the spring controller class which handles the request, processes it and returns back to the client.
@Controller annotation declares this as a controller class.
@RequestMapping("/articles") annotation tells Spring to process all requests beginning with /articles.
@RequestMapping(method = RequestMethod.GET) annotation tells Spring to process the request url /articles.html.
package net.roseindia.controller; |
articlesList.jsp
This jsp file is called when /articles.html is requested.
<%@ 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> |
<%@ 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> |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 3 MVC and Hibernate 3 Example application using Annotations</title> </head> <body> <h1>Spring 3 MVC and Hibernate 3 Example application using Annotations</h1> <a href="articles.html">List of Articles</a> <a href="articles/add.html">Add Article</a> </body> </html> |
When we compile the application and run, it displays the output as below:
Click on List of Articles link. It displays all articles as below:
Click on Add Article link, it let you add article in database.