Home Tutorial Spring Spring3 Web Spring 3 MVC and Hibernate 3 Example Part 3

 
 

Spring 3 MVC and Hibernate 3 Example Part 3
Posted on: September 7, 2010 at 12:00 AM
Advertisement
This tutorial explains how to use annotations with spring 3 MVC and hibernate 3 based application to make the development easier and faster than ever before.

 ArticleService.java

This is the interface which declares methods which will be used in controller class.

package net.roseindia.service;

import java.util.List;

import net.roseindia.model.Article;

public interface ArticleService {

  public void addArticle(Article article);

  public List<Article> listArticles();
}

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;

import java.util.List;

import net.roseindia.dao.ArticleDao;
import net.roseindia.model.Article;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@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();
  }

}

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;

import java.util.HashMap;
import java.util.Map;

import net.roseindia.model.Article;
import net.roseindia.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@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.addArticlearticle);
    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");
  }

}

 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>


addArticle.jsp
This jsp file is called when articles/add.html is requested.

<%@ 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>


index.jsp
This is the index page which is called by default for application.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="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:

index

Click on List of Articles link. It displays all articles as below:

list

Click on Add Article link, it let you add article in database.

add

Download the application

Advertisement

Related Tags for Spring 3 MVC and Hibernate 3 Example Part 3:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 7, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com