简体   繁体   中英

Why does 'update()' not redirect to 'sox' using @ModelAttribute in Spring MVC 3.0?

At the moment I just want to test redirecting from the update method back to the sox method. But instead I get an error complaining about a missing "update.jsp" .

@RequestMapping(value = "/sox/update", method = RequestMethod.POST)
@ModelAttribute("formWrapper")
public final String update(HttpServletRequest request, 
 @ModelAttribute("formWrapper") FormWrapper formWrapper,
    BindingResult bindResult,
    ModelMap model)
{
    return "redirect:/sox";
}

@ModelAttribute("formWrapper")
FormWrapper setupForm()
{
    FormWrapper formWrapper = new FormWrapper();
    return formWrapper;
}

@RequestMapping(value = "/sox", method = RequestMethod.GET)
public final String sox(HttpServletRequest request, ModelMap model)
{
    return "sox";
}

I think your problem is the @ModelAttribute on the update method. Try it this way:

@RequestMapping(value = "/sox/update", method = RequestMethod.POST)
    public final String update(HttpServletRequest request, 
     @ModelAttribute("formWrapper") FormWrapper formWrapper,
        BindingResult bindResult,
        ModelMap model)
    {
        return "redirect:/sox";
    }

When you add the @ModelAttribute to a method spring treats the return as a model attribute:

Any other return type is considered to be a single model attribute to be exposed to the view, using the attribute name specified through @ModelAttribute at the method level (or the default attribute name based on the return type class name). The model is implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.

Look at 15.3.2.8 of the Spring Docs for more info: (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM