2012-04-26 24 views
3

在Spring2.5我們寫控制器等如下:差:ModelAndView的

@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如何讓模型顯示? 謝謝:)

回答

2

春3.5尚未發佈,我希望你的意思春3.1。

return ModelAndView是一種舊式的代碼寫入,用於Spring 2.0。在Spring 3.0中添加以後,您可以返回ModelAndView或nameView。

我建議你總是返回字符串(視圖名稱),因爲Spring MVC中的一些高級功能將無法正常工作:

例子: Spring MVC 3.1 RedirectAttributes is not working