在application.xml中配置支持ModelAndView,配置方式有兩種。html
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <!-- 視圖文件的前綴 --> <property name="suffix" value=".jsp"></property> <!-- 視圖文件的後綴名 --> <!-- view是用什麼顯示,這裏是jsp,還能夠用velocity之類的 --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> </bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>
這種方式須要在beans中添加xmlns:p="http://www.springframework.org/schema/p"的支持。java
ModelAndView的構造方法有如下幾種。
web
ModelAndView() ModelAndView(View view) ModelAndView(String viewName, Map<String, ?> model) ModelAndView(View view, Map<String, ?> model) ModelAndView(String viewName, String modelName, Object modelObject) ModelAndView(View view, String modelName, Object modelObject)
ModelAndView預設是使用forward來轉發請求結果給視圖的,使用RedirectView會致使將請求從新導向視圖。spring
new ModelAndView(new RedirectView(this.getViewPage()));
viewPage的路徑是從根目錄算起,而不是web-inf算起,當指定viewName必須相對與根目錄的路徑。app
DispatcherServlet會根據傳回的ModelAndView來解析View名稱,並處理給予的Model。View名稱的解析是委託給實做org.springframework.web.servlet.ViewResolver接口的實例,ViewResolver接口定義以下:jsp
public interface ViewResolver { public view resolveViewName(String, Locale locale) throws ServletException; }
ViewResolver的一個實例是InternalResourceViewResolver,名稱解析完以後,實際的View繪製與Model轉換處理是交給實做org.springframework.web.servlet.View的實例,View接口以下:oop
public interface View { public void render(Map model, HttpServletResquest resquest, HttpServletResponse response) throws ServletException, IOException; }
View的實做以前用過org.springframework.web.servlet.view.InternalResourceView,另外也還有JstlView、TilesView、VelocityView等等的實做,分別進行不一樣的表現展處理。this
http://itroop.iteye.com/blog/263845
spa