SpringMVC-View Resolver

InternalResourceViewResolver

Controller
//...
public class WelcomeController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("WelcomePage");

        return model;
    }
}
InternalResourceViewResolver配置
<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 
   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" ></bean>

    <!-- Register the bean -->
    <bean class="com.mkyong.common.controller.WelcomeController" ></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>
        </bean>

</beans>
解析结果

prefix + view name + suffix = /WEB-INF/pages/WelcomPage.jsp

XmlViewResolver

根据xml文件中配置进行解析
默认文件位置: /WEB-INF/views.xml,
自定义文件位置:

<beans ...>
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
       <property name="location">
        <value>/WEB-INF/spring-views.xml</value>
       </property>
    </bean>
</beans>
配置View Beans
<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" ></property>
    </bean>

</beans>
配置XmlViewResolver
<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 
   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" ></bean>

    <!-- Register the bean -->
    <bean class="com.mkyong.common.controller.WelcomeController" ></bean>

    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
       <property name="location">
           <value>/WEB-INF/spring-views.xml</value>
       </property>
    </bean>

</beans>
完成Controller
public class WelcomeController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("WelcomePage");

        return model;
    }
}

ResourceBundleViewResolver

根据xml文件中配置进行解析
默认文件位置: classpath 根目录下views.properties,
自定义文件位置:

<beans ...>
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename" value="spring-views" ></property>
    </bean>
</beans>
配置View Beans

spring-views.properties

WelcomePage.(class)=org.springframework.web.servlet.view.JstlView
WelcomePage.url=/WEB-INF/pages/WelcomePage.jsp
配置ResourceBundleViewResolver
<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 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" ></bean>

    <!-- Register the bean -->
    <bean class="com.mkyong.common.controller.WelcomeController" ></bean>

    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename" value="spring-views" ></property>
    </bean>

</beans>
完成Controller
public class WelcomeController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("WelcomePage");

        return model;
    }
}

多个Resolver配置方式

<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" ></property>
    </bean>

    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
         <property name="basename" value="spring-views" ></property>
         <property name="order" value="1" ></property>
    </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" ></property>
        </bean>
</beans>

优先级顺序

XmlViewResolver --> ResourceBundleViewResolver --> InternalResourceViewResolver
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s