Set up a maven web project in Intellij with Spring and JPA - Part Seven

Welcome to part Seven of this tutorial series! After configuring our Spring MVC setup in the last tutorial we will now implement it. First of all let's have a look at our index.jsp and change it to this:


We only need this one line of code here.



Now create PostController.java in the blog.controller.dao package and mark it with @Controller.

package blog.controller;

import blog.domain.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Controller
public class PostController {

    @Autowired
    private PostService postService;

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String showHomepage(Model model, HttpServletRequest request) {
          request.setAttribute("currentPageItem",pageItem);
        List<Post> posts = postService.getMostRecentPosts();
        model.addAttribute("posts", posts);
        return "home";
    }

}

The showHompage method will be mapped to our forward in index.jsp, grab the most recent database from the database and show them on home.jsp.
Remember that we used a view resolver that points to /WEB-INF/views/ so create home.jsp under that directory.
Since we do not have PostService yet we will create it in the next step. It will basicly just call a method from our PostDao.
Now, create the class PostService in the package blog.service which looks like this:
package blog.service;

import blog.dao.PostDao;
import blog.domain.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class PostService {

    @Autowired
    private PostDao postDao;

    public List<Post> getMostRecentPosts() {
        return postDao.findMostRecentPosts();
    }
}

To test if everything so far is working we will add the following code to home.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

Maven webapp


Welcome to JavaBlog!

Most recent articles:

Posted on ${post.postDate}


${post.text}

Try running your application using a local tomcat server. The result will be a BeanCreationException with the following root cause:
org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
For more info please refer to: AopConfigException

That's all! if you add a post to the database you should now see it on the page.
One last thing I would like to share with you all is the following @PathVariable usage:
    @RequestMapping(value = "/posts/{postId}", method = RequestMethod.GET)
    public String findPostbyId(@PathVariable long postId, Model model, HttpServletRequest request) {
        request.setAttribute("currentPageItem",pageItem);
        Post post = postService.getPostByPostId(postId);
        model.addAttribute(post);
        return "displayPost";
    }

The path variable will extract the postId from the url and put it in a local variable called postId.
That's it for these series of tutorial, hope you enjoyed it and learned a thing or two :)

More about this tutorial series can be found here:
Set up a maven web project in Intellij with Spring and JPA - Part One (Project setup)
Set up a maven web project in Intellij with Spring and JPA - Part Two (Maven setup)
Set up a maven web project in Intellij with Spring and JPA - Part Three (ORM mapping)
Set up a maven web project in Intellij with Spring and JPA - Part Four (Spring Configuration)
Set up a maven web project in Intellij with Spring and JPA - Part Five (DAO)
Set up a maven web project in Intellij with Spring and JPA - Part Six (Spring MVC Configuration)