需要1个MVC控制器用于2个功能(创建实体和上传img),它们是分开的

时间:2015-02-17 09:57:05

标签: java spring-mvc file-upload model-view-controller controller

我花了2天时间找到问题的解决方案,但没有得到任何结果。如果你能给我一个很好的例子链接 - 请发布,因为我没有找到任何...如果你需要更多关于我的问题的信息 - 请告诉我更多细节!所以,问题是:我有2个控制器(第一个用于创建实体,第二个用于为此实体上载img)。现在我需要创建一个实体,可以只用一个控制器为这个实体上传img。 因此,实体创建的控制器代码是:

 @RequestMapping(value = "/groups", method = RequestMethod.POST)
    public String doAddGroup(Model model,
            @ModelAttribute("group") @Valid final Group group,
            final BindingResult result, RedirectAttributes redirectAttributes,
            HttpSession httpSession) {
        if (result.hasErrors()) {
            redirectAttributes.addFlashAttribute(
                    "org.springframework.validation.BindingResult.group",
                    result);
            redirectAttributes.addFlashAttribute("group", group);
            redirectAttributes.addFlashAttribute("groupNotCreated", true);
            return "redirect:/groups";
        }
        groupService.create(group);
        redirectAttributes.addAttribute("id", group.getId()).addFlashAttribute(
                "groupCreated", true);
        return GROUP_LINK;
    }

img上传的控制器代码是:

 @RequestMapping(value = "/add-group-img", method = RequestMethod.POST)
    public String addGroupPhoto(@RequestParam("file") MultipartFile[] file,
            @ModelAttribute("group") final Group group,
            RedirectAttributes redirectAttributes) {
        redirectAttributes.addAttribute("id", group.getId());
        imageService.addImageToGroup(file, group);
        return GROUP_LINK;
    }

现在一切正常,但是我需要让用户有机会同时使用img创建组,而不是通过不同的操作。当我试图将MultipartFile设置为实体创建控制器时,它不起作用。请解释我如何通过仅更改控制器来解决它,是否可能? 提前致谢! :)

0 个答案:

没有答案