在Spring mvc中的另一个控制器方法中调用控制器方法

时间:2016-10-19 06:14:03

标签: spring spring-mvc model-view-controller controller url-redirection

我有一个控制器类,用于处理向系统添加新项目。为此,首先我在控制器方法中填充下拉列表,如下所示:

 @RequestMapping(value = "/getSubcats", method = RequestMethod.POST)
    public
    @ResponseBody
    List<String> getSubcatList(@RequestParam("categoryNm") String categoryNm) {

        return subCategoryRepository.retrieveSubcatogories(categoryNm);
    }

然后我有一个控制器方法来向用户查看添加项目表单,这是正确填充该下拉列表的位置。像这样:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String showAddItem(Model model) {

    List<String> listCat = categoryRepository.selectCategoryNames();

    model.addAttribute("listCat", listCat);
    model.addAttribute("command", new Item());
    return "item_management/addItem";
}

在提交添加新项目表格之后,我写了另一种方法。在那里,如果用户输入了现有项目的名称,我需要在单击提交按钮时将所有填充数据重定向到同一表单。但是当我使用以下方法执行此操作时,下拉列表不会填充并且为空。我想我需要在方法"/getSubcats"中调用名为"/add_item"的控制器方法。是正确的,如果不是,我该怎么做?

//控制器方法用于提交添加新项目

@RequestMapping(value = "/add_item")
    public ModelAndView addItem(@ModelAttribute("newItem") Item newItem) throws SQLIntegrityConstraintViolationException {

        String itemNm = newItem.getItemName();
        String itemPrice = newItem.getPrice();
        String itemSize = newItem.getSize();

        List<String> sizelist = new ArrayList<String>(Arrays.asList(itemSize.split(",")));
        List<String> pricelist = new ArrayList<String>(Arrays.asList(itemPrice.split(",")));

        List<Item> items = new ArrayList<Item>();

            for(int s =0; s<sizelist.size(); s++){

              Item listItem = new Item();
                    listItem.setSize(sizelist.get(s));
                    listItem.setPrice(pricelist.get(s));
                items.add(listItem);

            }

        boolean uniqueItemNm = item.checkAvailability(itemNm);
        if (!uniqueItemNm) {
            int a = item.addItem(newItem, items);
            if (a != 1){
                JOptionPane.showMessageDialog(null, "Server-side error. Cannot add the item !", "Error !",
                    JOptionPane.ERROR_MESSAGE);
                LOGGER.error("Server-side error in adding item "+ itemNm);
            }
            else {
                JOptionPane.showMessageDialog(null, "Added new item " + itemNm, "Success",
                        JOptionPane.INFORMATION_MESSAGE);
                LOGGER.info("added New Item to database " + itemNm);
                return new ModelAndView(new RedirectView("add"));
            }
        } else{
            JOptionPane.showMessageDialog(null,
                    "Item name is already exists! " + itemNm, "Warning ",
                    JOptionPane.WARNING_MESSAGE);

            newItem.setPrice(" ");  LOGGER.trace("after clear price"+ itemPrice );
            newItem.setSize(" ");  LOGGER.trace("after clear size" + itemSize);
            newItem.getCategoryName();
            //return "redirect:/getUser";
            return new ModelAndView("item_management/addItem", "command", newItem);
        }
       return new ModelAndView(new RedirectView("add"));
    }

0 个答案:

没有答案