日期:2014-05-16  浏览次数:20425 次

Spring MVC 中如何设置使用多视图解析器--JSP与FreeMarker

首先是在Web.xml 中配置请求分法器 代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
??? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
??? <listener>
??? ??? <listener-class>
??? ??? ??? org.springframework.web.context.ContextLoaderListener
??? ??? </listener-class>
??? </listener>
??? <!-- ======================================================== -->
??? <!--????????????????? Spring MVC Config Servlet?????????????? -->
??? <!-- ======================================================== -->
??? <!-- JSP DispatcherServlet -->
??? <servlet>
??? ??? <servlet-name>jspdpt</servlet-name>
??? ??? <servlet-class>
??? ??? ??? org.springframework.web.servlet.DispatcherServlet
??? ??? </servlet-class>
??? ??? <load-on-startup>1</load-on-startup>
??? </servlet>
??? <!-- FreeMarker DispatcherServlet -->
??? <servlet>
??? ??? <servlet-name>ftldpt</servlet-name>
??? ??? <servlet-class>
??? ??? ??? org.springframework.web.servlet.DispatcherServlet
??? ??? </servlet-class>
??? ??? <load-on-startup>2</load-on-startup>
??? </servlet>

??? <!-- ======================================================== -->
??? <!--????????????????? Spring MVC Config Mapping?????????????? -->
??? <!-- ======================================================== -->
??? <servlet-mapping>
??? ??? <servlet-name>jspdpt</servlet-name>
??? ??? <url-pattern>*.view</url-pattern>
??? </servlet-mapping>

??? <servlet-mapping>
??? ??? <servlet-name>ftldpt</servlet-name>
??? ??? <url-pattern>*.ftl</url-pattern>
??? </servlet-mapping>

??? <welcome-file-list>
??? ??? <welcome-file>index.html</welcome-file>
??? </welcome-file-list>

??? <jsp-config>
??? ??? <taglib>
??? ??? ??? <taglib-uri>/spring</taglib-uri>
??? ??? ??? <taglib-location>/WEB-INF/spring.tld</taglib-location>
??? ??? </taglib>
??? </jsp-config>
</web-app>
然后为不同的servlet 映射编写不同的Spring IOC 容器:
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xmlns:util="http://www.springframework.org/schema/util"
??? xsi:schemaLocation="
??? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
??? http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

??? <!-- ===================================================== -->
??? <!--?????????? ViewResolver For FreeMarker???????????????? -->
??? <!-- ===================================================== -->
??? <bean id="viewResolver"
??? ??? class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
??? ??? <property name="cache">
??? ??? ??? <value>true</value>
??? ??? </property>
??? ??? <property name="suffix">
??? ??? ??? <value>.ftl</value>
??? ??? </property>
??? ??? <property name="exposeRequestAttributes">
??? ??? ??? <value>true</value>
??? ??? </property>
??? ??? <property name="exposeSessionAttributes">
??? ??