SpringMVC中的视图解析器的主要作用就是将逻辑视图转换成用户可以看到的物理视图。
当用户对SpringMVC应用程序发起请求时,这些请求都会被SpringMVC的DispatcherServlet处理,通过处理器找到最为合适的HandlerMapping定义的请求映射中最为合适的映射,然后通过HandlerMapping找到相对应的Handler,然后再通过相对应的HandlerAdapter处理该Handler。返回结果是一个ModelAndView对象,当该ModelAndView对象中不包含真正的视图,而是一个逻辑视图路径的时候,ViewResolver就会把该逻辑视图路径解析为真正的View视图对象,然后通过View的渲染,将最终结果返回给用户。
SpringMVC中处理视图最终要的两个接口就是ViewResolver和View,ViewResolver的作用是将逻辑视图解析成物理视图,View的主要作用是调用其render()方法将物理视图进行渲染。
一般来说,对于SpringMVC控制器中的方法,无论是返回String、View或者是ModelAndView,SpringMVC在内部都会将返回结果封装成ModelAndView对象,然后返回给用户。
视图解析器需要配置在SpringMVC的配置文件中,如下:
1
2
3
4
5
6
< bean id = “viewResolver”
class = “org.springframework.web.servlet.view.InternalResourceViewResolver” >
< property name = “prefix” value = “/page” ></ property >
< property name = “suffix” value = “.jsp” ></ property >
</ bean >
SpringMVC视图解析器解析流程:
1、将SpringMVC控制器中的返回结果封装成一个ModelAndView对象。
2、通过SpringMVC中的视图解析器,使用ViewResolver对控制器返回的ModelAndView对象进行解析,将逻辑视图转换成物理视图。
3、调用View中的render()方法对物理视图进行渲染
U2FsdGVkX19nJ15D1grTspd6yA2CIhWOYLOBMnHrxSPalgzq1MmsYnSFP3aokDFi
gD6M1RV7XzSzCiL21vRigCN2eed6ogbxanjNCFSJ5WpBmUOOX57lLKKjBIyYqFRi
DPozP57BadzrHRIO2aF9L61RBEdLkO8OthtCKJP9KZxQzu9cQgmVswfYe31Vv5cY
L3v6dEFUkF2r3xU6qTzNfJpMZE9+ofoLb0J1FbheicmPn7j8FKIgng6wlSKS2KNy
2o28aa8OFBRb3TnK6i/9sjQX8XPMac5rSXzXc7pPYXcfiXqEqn13xyNZzPRew2XJ
GqmCazVr3TXhSsxiSPOu1j0NR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