FreeMarker 多视图配置

(1)新增自定义ViewResolver
package com.zcrm.resolver;
import java.util.Locale;
import java.util.Map;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
public class MultViewResolver implements ViewResolver {
 private Map<String, ViewResolver> resolvers;
 @Override
 public View resolveViewName(String viewName, Locale locale) throws Exception {
  int n = viewName.lastIndexOf(".");
  String suffix = "";
  if (n == (-1)) {
   suffix = "jsp";
  } else {
   suffix = viewName.substring(n + 1);
   viewName = viewName.substring(0, n);
  }
  // 根据下划线后面的字符串去获取托管的视图解析类对象
  ViewResolver resolver = resolvers.get(suffix);
  if (resolver != null) {
   return resolver.resolveViewName(viewName, locale);
  } else {
   return null;
  }
 }
 public Map<String, ViewResolver> getResolvers() {
  return resolvers;
 }
 public void setResolvers(Map<String, ViewResolver> resolvers) {
  this.resolvers = resolvers;
 }
}
 
 
(2)修改spring-mvc.xml ,使用自定义 ViewResolver
 <bean id="zcrmViewResolver" class="com.zcrm.resolver.MultViewResolver">
  <property name="resolvers">
   <map>
    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <entry key="jsp">
     <bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass"
       value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/WEB-INF/jsp/pages/" />
      <property name="suffix" value=".jsp" />
     </bean>
    </entry>
   
    <entry key="ftl">
     <bean id="viewResolverFtl"
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="viewClass"
       value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
      <property name="contentType" value="text/html; charset=utf-8" />
      <property name="cache" value="true" />
      <property name="prefix" value="" />
      <property name="suffix" value=".ftl" />
     </bean>
    </entry>
   </map>
  </property>
 </bean>
 
 <!-- ******************************freeMarker配置【START】****************************** -->
 <bean id="freemarkerConfig"
  class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPath" value="/WEB-INF/jsp/ftl/" />
  <property name="freemarkerVariables">
   <map>
    <entry key="xml_escape" value-ref="fmXmlEscape" />
   </map>
  </property>
  <property name="defaultEncoding" value="utf-8" />
  <property name="freemarkerSettings">
   <props>
    <prop key="template_update_delay">3600</prop>
   </props>
  </property>
 </bean>
 <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
 <!-- ******************************freeMarker配置【END】****************************** -->
 
(3)新增freemarker.jar
 
(4)编写ftl文件
datatables.ftl
 
(5)编写测试Controller
package com.zcrm.controller.crm2.freemarker;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/crm2/freemarker")
public class Crm2FreeMarkerController {
 
 @RequestMapping(value={"/init"},method=RequestMethod.GET)
    public String updatePassword(HttpServletRequest req, Model model) {
  return "crm2/datatables.ftl";
    }
 
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(freemarker)