Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RequestMapping(value = "/add", method = RequestMethod.POST)
- public String processAddNewProductForm(@ModelAttribute("newProduct") Product productToBeAdded, BindingResult result,
- HttpServletRequest request) {
- String[] suppressedFields = result.getSuppressedFields();
- if (suppressedFields.length > 0) {
- throw new RuntimeException("Attempting to bind disallowed fields: "
- + StringUtils.arrayToCommaDelimitedString(suppressedFields));
- }
- MultipartFile productImage = productToBeAdded.getProductImage();
- String rootDirectory = request.getSession().getServletContext().getRealPath("/");
- if (productImage != null && !productImage.isEmpty()) {
- try {
- productImage.transferTo(
- new File(rootDirectory + "/resources/images/" + productToBeAdded.getProductId() + ".png"));
- } catch (Exception e) {
- throw new RuntimeException("Product Image saving failed", e);
- }
- }
- MultipartFile pdfManual = productToBeAdded.getPdfManual();
- if (pdfManual != null && !pdfManual.isEmpty()) {
- try {
- pdfManual.transferTo(
- new File(rootDirectory + "/resources/pdfManuals/" + productToBeAdded.getPdfManual() + ".pdf"));
- } catch (Exception e) {
- throw new RuntimeException("Product manual saving failed", e);
- }
- }
- productService.addProduct(productToBeAdded);
- return "redirect:/products";
- }
Advertisement
Add Comment
Please, Sign In to add comment