Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.amuponda.estorehack.client.web.controller; import com.amuponda.estorehack.business.domain.Category; import com.amuponda.estorehack.business.domain.Product; import com.amuponda.estorehack.business.service.CategoryService; import com.amuponda.estorehack.business.service.ProductService; import java.beans.PropertyEditorSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; import static org.springframework.web.bind.annotation.RequestMethod.POST; /** * * @author amuponda */ @Controller @RequestMapping("/products") public class ProductController { @Autowired CategoryService categoryService; @Autowired ProductService productService; /** * Requests the new product form * @param model * @return */ @RequestMapping(value = "/add", method = GET) public String getProductForm(Model model) { model.addAttribute(new Product()); model.addAttribute(categoryService.findAll()); return "product/productForm"; } /** * Saves the new product to the database * @param product * @return */ @RequestMapping(value = "/add", method = POST) public String processProduct(Product product) { productService.save(product); return "redirect:/"; } @InitBinder public void initBinder(WebDataBinder webDataBinder) { webDataBinder.registerCustomEditor(Category.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { setValue(categoryService.findByName(text)); } }); } }