`
sharkl
  • 浏览: 40431 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类

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">
            <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 的合作工作,前提条件是根据後最名称来进行请求的分发

分享到:
评论

相关推荐

    liferay-spring-mvc-freemarker-portlet-archetype

    liferay-spring-mvc-freemarker-portlet-archetype 这是用于创建 Liferay Spring MVC Freemarker portlet 的 maven 原型。 由于 Liferay 不正式支持这种类型的 portlet,因此当前不存在原型。 这个原型修正了这一点...

    spring3.0MVC中文教程.pdf

    Spring MVC是Spring的框架的... 这意味着,你甚至可以告诉Spring使用Web框架中的任何一个。 比Spring更不紧耦合的servlet或JSP向客户端呈现视图。 喜欢速度与其他视图技术集成,Freemarker的,Excel或PDF现在也有可能。

    spring3.0MVC中文教程

    Spring MVC是Spring的框架的... 这意味着,你甚至可以告诉Spring使用Web框架中的任何一个。 比Spring更不紧耦合的servlet或JSP向客户端呈现视图。 喜欢速度与其他视图技术集成,Freemarker的,Excel或PDF现在也有可能。

    spring mvc 3.2 参考文档

    Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...

    spring3.0 MVC 中文教程

    Spring MVC是Spring的框架的... 这意味着,你甚至可以告诉Spring使用Web框架中的任何一个。 比Spring更不紧耦合的servlet或JSP向客户端呈现视图。 喜欢速度与其他视图技术集成,Freemarker的,Excel或PDF现在也有可能。

    spring3.0_MVC之中文教程

    Spring MVC是Spring的框架的... 这意味着,你甚至可以告诉Spring使用Web框架中的任何一个。 比Spring更不紧耦合的servlet或JSP向客户端呈现视图。 喜欢速度与其他视图技术集成,Freemarker的,Excel或PDF现在也有可能

    Spring3 MVC

    Spring MVC是Spring的框架的... 这意味着,你甚至可以告诉Spring使用Web框架中的任何一个。 比Spring更不紧耦合的servlet或JSP向客户端呈现视图。 喜欢速度与其他视图技术集成,Freemarker的,Excel或PDF现在也有可能。

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring4.3.9相关jar包

    Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 外部依赖Commons Logging, (Log4J)。 spring-beans.jar(必须):这 个jar 文件是所有应用都要...

    基于java的企业级应用开发:Spirng MVC入门.ppt

    它支持JSP、Velocity和FreeMarker等视图技术。 使用基于XML的配置文件,在编辑后,不需要重新编译应用程序。 在Eclipse中,创建一个名称为chapter11的Web项目,在项目的lib目录中添加运行Spring MVC程序所需要的JAR...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring in Action(第二版 中文高清版).part2

    16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...

    Spring in Action(第二版 中文高清版).part1

    16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...

    rural:基于spring的mvc框架,使用简单,URL映射零配置零注解

     目前Rural支持json,jsp,freemarker三种视图。 #####1.2 Hello World  首先在web.xml中配置,类似spring mvc,需要配置listener来启动spring context,配置servlet负责请求分发 &lt;param&gt;contextConfigLocation...

    OpenShop:基于jeeshop的开源B2C商店系统

    使用spring mvc替换strutus2 使用freemarker替换jsp 使用maven替换传统的项目结构管理 将免费进行得更彻底:使用()替换easyui(因为easyui是GPL协议) 更加紧密地拥抱bootstrap:后台管理界面使用() OpenShop技术选型 ...

    SSH相关整合的总要资料

    是一个为开发基于模型(Model)-视图(View)-控制器(Controller)(MVC)模式的应用架构的开源框架,是利用Java Servlet和JSP构建Web应用的一项非常有用的技术。其官方网站如下: 是一个开源框架,是为了解决企业应用...

Global site tag (gtag.js) - Google Analytics