/** * Example URL: http://localhost:8080/webstore/products/add * * <p>Called when a POST is requested * * @param newProduct * @return */ @RequestMapping(value = "/add", method = RequestMethod.POST) public String processAddNewProductForm( @ModelAttribute("newProduct") @Valid Product newProduct, BindingResult result, HttpServletRequest request) { if (result.hasErrors()) { return "addProduct"; } String[] suppressedFields = result.getSuppressedFields(); if (suppressedFields.length > 0) { throw new RuntimeException( "Attempting to bind disallowed fields: " + StringUtils.arrayToCommaDelimitedString(suppressedFields)); } MultipartFile productImage = newProduct.getProductImage(); MultipartFile productManual = newProduct.getProductManual(); String rootDirectory = request.getSession().getServletContext().getRealPath("/"); if (productImage != null && !productImage.isEmpty()) { try { productImage.transferTo( new File(rootDirectory + "resources\\images\\" + newProduct.getProductId() + ".jpg")); } catch (Exception e) { throw new RuntimeException("Product Image saving failed", e); } } if (productManual != null && !productManual.isEmpty()) { try { productManual.transferTo( new File(rootDirectory + "resources\\pdf\\" + newProduct.getProductId() + ".pdf")); } catch (Exception e) { throw new RuntimeException("Product Manual saving failed", e); } } productService.addProduct(newProduct); return "redirect:/products"; }