spring 3 mvc中的view resolver小结_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > spring 3 mvc中的view resolver小结

spring 3 mvc中的view resolver小结

 2010/12/29 8:11:01  jackyrong  http://jackyrong.javaeye.com  我要评论(0)
  • 摘要:下面小结下spring3mvc中的viewresolver:1InternalResourceViewResolver这是最常用的了,用来拼接返回的视图,比如:<beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/><!--Registerthebean--><beanclass="com.mkyong.common.controller
  • 标签:MVC view Spring
下面小结下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
发表评论
用户名: 匿名