Spring 2.5和Spring 3.1之间的区别:ModelAndView

时间:2012-04-26 06:34:56

标签: spring spring-mvc

在Spring2.5中,我们编写如下控制器:

@Controller
public class HelloWorldController{

 @RequestMapping(value="/welcome",method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    ModelAndView model = new ModelAndView("hello");
    model.addObject("msg", "hello world");

    return model;
}

}

在Spring 3.1中:

@RequestMapping(value="/welcome",method = RequestMethod.GET)
public String printWelcomeString(ModelMap model) {

    model.addAttribute("message", "hello world);
    return "hello";

}

printwelcomeString()函数返回一个String而不是ModelAndView。

任何人都可以解释一下吗? 它是如何运作的? hello.jsp如何显示模型? 谢谢:))

1 个答案:

答案 0 :(得分:3)

Spring 3.5尚未发布我希望你的意思是Spring 3.1。

return ModelAndView是一种旧式代码编写,在Spring 2.0中使用。在Spring 3.0中添加以后,您可以返回ModelAndView或nameView。

我建议你总是返回String(视图名称),因为Spring MVC的一些高级功能将无法正常工作:

实施例: Spring MVC 3.1 RedirectAttributes is not working