Spring MVC重定向问题

时间:2014-11-12 12:16:38

标签: java spring spring-mvc

我在Spring重定向中面临问题。这是我第一次在spring mvc 4.1中使用modelandview。下面是代码段

@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
    private ModelAndView deleteProduct(@PathVariable Integer id,final RedirectAttributes redirectAttributes)
    {
        logger.info("Deleting product from database..."+id);
        ModelAndView modelAndView = new ModelAndView("redirect:home");
        redirectAttributes.addFlashAttribute( "message","Product was successfully deleted.");
        return modelAndView;
    }

它进入上面的方法并抛出一个错误说客户端发送的请求在语法上是不正确的()。 (400错代码)。如果我删除重定向如下,它工作正常。但我需要重定向!

ModelAndView modelAndView = new ModelAndView("home");

家庭映射代码

@RequestMapping(value="/home", method=RequestMethod.GET)
    private ModelAndView home(@ModelAttribute Product product) {
        return new ModelAndView("home");
    }

但是下面的代码可以正常用于重定向(其他功能)

@RequestMapping(value="/addproduct", method=RequestMethod.POST)
    private ModelAndView addingProduct(@ModelAttribute Product product,final RedirectAttributes redirectAttributes) {
        logger.info("Product adding to database...");
        ModelAndView modelAndView = new ModelAndView("redirect:home");
        redirectAttributes.addFlashAttribute( "message","Product was successfully added.");
        return modelAndView;
    }

请帮助我理解问题

2 个答案:

答案 0 :(得分:0)

尝试这种方式:

ModelAndView modelAndView = new ModelAndView("redirect:/home"); // from /delete/{id} Path

答案 1 :(得分:0)

使用ModelAndView modelAndView = new ModelAndView(" redirect:/");