Spring MVC JSP和freemaker整合

本文介绍如何在SpringMVC框架中整合Freemarker模板引擎,并通过配置优先级实现Freemarker与JSP的共存。展示了具体的配置文件示例,以便根据不同需求选择合适的视图解析方式。

关于springmvc如何整合freemaker 可以参考 http://my.oschina.net/bddiudiu/blog/228788


但是如果只是发布到前端的页面是需要静态化 即使用freemaker来生成 后台的操作部分的页面还是用jsp这时候我们应该怎么做呢...


我们看一下 springMVC-servlet.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring   
  http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
    
        <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射   请求映射-->  
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
	 
	<property name="messageConverters">   
	         <list>   
	             <bean class = "org.springframework.http.converter.StringHttpMessageConverter">   
	                <property name = "supportedMediaTypes">
	                      <list>
	                          <value>text/html;charset=UTF-8</value>   
	                     </list>   
	                </property>   
	             </bean>   
	         </list>   
	   </property>  
	</bean>	
    	
    	<!--对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> 
      <context:component-scan base-package="com.youto.controller"/>
      
      <mvc:annotation-driven />
      
      <!-- 静态文件目录 -->
      <mvc:resources location="/assets/" mapping="/assets/**" />
      <mvc:interceptors>
          <mvc:interceptor>
			<mvc:mapping path="/manager/**"/>              
			<bean class="com.youto.util.ManagerInterceptor" />
          </mvc:interceptor>
      </mvc:interceptors>
      
      <!--以下三种视图配置根据需要任选一种即可 --> 
      
     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
     <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
     	<property name="prefix" value="/WEB-INF/views/"/>
    	<property name="suffix" value=".jsp"/>
     </bean>-->
     
     <!-- 针对freemarker的视图配置 -->  
    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
        <property name="cache" value="true" />  
        <property name="prefix" value="" />  
        <property name="suffix" value=".ftl" />  
        <property name="contentType" value="text/html;charset=UTF-8"></property>  
        <property name="requestContextAttribute" value="request" />  
        <property name="exposeSpringMacroHelpers" value="true" />  
        <property name="exposeRequestAttributes" value="true" />  
        <property name="exposeSessionAttributes" value="true" />  
    </bean>
    
    <!-- View resolvers can also be configured with ResourceBundles or XML files.   
        If you need different view resolving based on Locale, you have to use the   
        resource bundle resolver. -->  
    <!-- 这个是针对返回视图还是json值的视图配置   来分别处理同步和异步请求 -->  
    <!--<bean  
            class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
            <property name="mediaTypes">  
                <map>  
                    <entry key="html" value="text/html" />  
                    <entry key="json" value="application/json" />  
                </map>  
            </property>  
            <property name="favorParameter" value="true" />  
            <property name="viewResolvers">  
                <list>  
                    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />  
                    <bean id="viewResolver"  
                        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
                        <property name="cache" value="true" />  
                        <property name="prefix" value="" />  
                        <property name="suffix" value=".ftl" />  
                        <property name="contentType" value="text/html;charset=UTF-8"></property>  
                        <property name="requestContextAttribute" value="request" />  
                        <property name="exposeSpringMacroHelpers" value="true" />  
                        <property name="exposeRequestAttributes" value="true" />  
                        <property name="exposeSessionAttributes" value="true" />  
                    </bean>  
                </list>  
            </property>  
            <property name="defaultContentType" value="text/html" />  
        </bean>  
        -->  
     
</beans>

这时候我们只需要在 每个视图配置里面添加 <property name="order" value="orderValue"/> 这个属性

这个配置表示解析器的优先级别。我们将FreeMarkerViewResolver的级别设为0,将InternalResourceViewResolver的级别设为1。这样,解析器就会优先使用 FreeMarkerViewResolver 进行解析,如果找不到相应的模板,就使用InternalResourceViewResolver进行解析,如果还找不到页面,就会产生一个404错误!

具体实例 稍后更新

转载于:https://my.oschina.net/bddiudiu/blog/228877

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值