首先是在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">
<value>true</value>
</property>
<property name="exposeSpringMacroHelpers">
<value>true</value>
</property>
<property name="contentType">
<value>text/html; charset=GB2312</value>
</property>
</bean>
<!-- ===================================================== -->
<!-- ViewResolver For FreeMarkerConfigurer -->
<!-- ===================================================== -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/WEB-INF/FTL/</value>
</property>
<property name="defaultEncoding">
<value>GB2312</value>
</property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">3600</prop>
</props>
</property>
</bean>
<bean id="welcomeController" class="sprinl.mvc.WelcomeController">
<property name="viewName">
<value>welcome</value>
</property>
</bean>
<bean id="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/welcome.ftl">welcomeController</prop>
</props>
</property>
</bean>
</beans>
以及
<?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 JSP -->
<!-- ===================================================== -->
<bean id="JSPViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="divfix">
<value>/WEB-INF/JSP/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="IndexController" class="sprinl.mvc.IndexController">
<property name="messages">
<value>我的第一个Spring-View例子--要给我鼓励</value>
</property>
<property name="page">
<value>home</value>
</property>
</bean>
<bean id="divsonController" class="sprinl.mvc.PresonController">
<property name="page">
<value>divson</value>
</property>
</bean>
<bean id="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/home.view">IndexController</prop>
<prop key="/divson.view">divsonController</prop>
</props>
</property>
</bean>
</beans>
通过上述代码 就可以实现JSP以及FreeMarker 的合作工作,前提条件是根据後最名称来进行请求的分发