下面小结下spring 3 mvc中的view resolver:
1 InternalResourceViewResolver
这是最常用的了,用来拼接返回的视图,比如:
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- Register the bean -->
<bean class="com.mkyong.common.controller.WelcomeController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
则返回的以:prefix + view name + suffix = /WEB-INF/pages/WelcomPage.jsp
2) XmlViewResolver
看
例子:
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- Register the bean -->
<bean class="com.mkyong.common.controller.WelcomeController" />
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
</bean>
spring-views.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="WelcomePage"
class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/pages/WelcomePage.jsp" />
</bean>
</beans>
其实就是将view的配置分开来配置而已,容易明白
3 ResourceBundleViewResolver
看例子:
<bean class="com.mkyong.common.controller.WelcomeController" />
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="spring-views" />
</bean>
这个时候,实际上去CLASS目录下的spring-views.properties去找相关的view,view
的配置如下:
WelcomePage.(class)=org.springframework.web.servlet.view.JstlView
WelcomePage.url=/WEB-INF/pages/WelcomePage.jsp
4 view resolvers 的优先级
<beans ...>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="spring-views" />
<property name="order" value="1" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="2" />
</bean>
</beans>
注意的是,数字越低,优先级越高,则以上的顺序为:
XmlViewResolver --> ResourceBundleViewResolver --> InternalResourceViewResolver