CRUD with Spring MVC & Hibernate

7. Create Controllers

In this tutorial, I will only show CategoryController. For the ProductController, I will post in separate article. Because in the Product Controller we will need additional class and configurations.

CategoryController

This controller will be processing request mapping for display all category data (list), add new category, edit category, and delete category. Make sure that you put this class in the same package with HomeController.

package org.munif.bookstore;

import org.munif.domain.Category;
import org.munif.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CategoryController {

<pre><code>// CategoryService object is autowired with CategoryServiceDB
@Autowired
private CategoryService categoryService;

@RequestMapping(value=&amp;quot;/category/&amp;quot;, method=RequestMethod.GET)
public ModelAndView categoryHome() {

    // set view: /category/list.jsp
    ModelAndView mav = new ModelAndView(&amp;quot;/category/list&amp;quot;);

    // return list of categories to be displayed on /category/list.jsp
    mav.addObject(&amp;quot;categories&amp;quot;, categoryService.findAll());
    return mav;
}

@RequestMapping(value=&amp;quot;/category/add&amp;quot;, method=RequestMethod.GET)
public ModelAndView categoryAddForm() {

    // set view: /category/add.jsp
    ModelAndView mav = new ModelAndView(&amp;quot;/category/add&amp;quot;);

    // return Category object that will be binded by add.jsp as commandName
    // every input in form will be binded by this commandName
    mav.addObject(&amp;quot;categoryBean&amp;quot;, new Category());

    return mav;
}

@RequestMapping(value=&amp;quot;/category/addConfirm&amp;quot;, method=RequestMethod.POST)
public ModelAndView categoryAddConfirm(@ModelAttribute Category category) {

    // set view: redirect to /category/
    ModelAndView mav = new ModelAndView(&amp;quot;redirect:/category/&amp;quot;);

    // add new Category from form entry from ModelAttribute
    // ModelAttribute object comes from object that binded by commandName in the form
    categoryService.addCategory(category);

    return mav;
}

// {categoryId} --&amp;gt; path variable from hyperlink.
// Example: /category/edit/1
@RequestMapping(value=&amp;quot;/category/edit/{categoryId}&amp;quot;, method=RequestMethod.GET)
public ModelAndView categoryEditForm(@PathVariable Integer categoryId) {
    // set view: /category/edit.jsp
    ModelAndView mav = new ModelAndView(&amp;quot;/category/edit&amp;quot;);

    // find desired category to edit by using categoryId
    Category categoryEdit = categoryService.findById(categoryId);       

    // return Category object that will be binded by edit.jsp as commandName
    // Different from add form, this object already have a value.
    mav.addObject(&amp;quot;categoryEdit&amp;quot;, categoryEdit);

    return mav;
}

@RequestMapping(value=&amp;quot;/category/edit/editConfirm&amp;quot;, method=RequestMethod.POST)
public ModelAndView categoryEditConfirm(@ModelAttribute Category category) {

    // set view: redirect to /category/
    ModelAndView mav = new ModelAndView(&amp;quot;redirect:/category/&amp;quot;);

    // Update Category from edit form
    categoryService.editCategory(category, category.getCategoryId());

    return mav;
}

@RequestMapping(value=&amp;quot;/category/delete/{categoryId}&amp;quot;, method=RequestMethod.GET)
public ModelAndView categoryAddConfirm(@PathVariable Integer categoryId) {

    ModelAndView mav = new ModelAndView(&amp;quot;redirect:/category/&amp;quot;);

    categoryService.removeCategory(categoryId);

    return mav;
}
</code></pre>

}
Advertisements

3 thoughts on “CRUD with Spring MVC & Hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s