FreaMarker入門教程,環境整合到使用例項

會當擊水三千里發表於2016-01-05
一、spring整合FreaMarker

1、下載freemarker.jar

2、配置web.xml

   <filter>
    <filter-name>openSessionInViewFilter</filter-name>
   <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	</filter>
此處需要注意自己的架包版本hibernate3、hibernate4

3、配置spring.xml

  <!-- Freemarker配置 -->
    <bean id="freemarkerConfig"  
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
    <property name="templateLoaderPath" value="/WEB-INF/views/" />  
    <property name="freemarkerSettings">  
        <props>  
            <prop key="template_update_delay">0</prop>  
            <prop key="default_encoding">UTF-8</prop>  
            <prop key="number_format">0.##########</prop>  
            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
            <prop key="classic_compatible">true</prop>  
            <prop key="template_exception_handler">ignore</prop>  
        </props>  
    </property>  
	</bean>
	<!--freemarker檢視直譯器 --> 
	<bean id="viewResolver"  
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
<property name="contentType" value="text/html; charset=utf-8"/>  
	<property name="suffix" value=".ftl"/> 
    <property name="cache" value="true" />  
    <property name="order" value="0" />
	</bean>  
	<!--springmvc普通檢視直譯器  -->  
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="1" />
    </bean> 
4、靜態頁面生產

模版hello.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>${title}</title>  
    </head>  
    <body>
        ${content}
    </body>  
</html>
控制器

@RequestMapping("/hello")
	public ModelAndView handleRequest(HttpServletRequest hsr  
            , HttpServletResponse hsr1) throws Exception {  
        ModelAndView mv = new ModelAndView("hello");  
        mv.addObject("title", "Spring MVC And Freemarker");  
        mv.addObject("content", " Hello world , test my first spring mvc ! ");  
        
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("title", "Spring MVC And Freemarker");
        root.put("content", "Hello world , test my first spring mvc !");
		String templatesPath = "E:/xkm/JKD/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/b/WEB-INF/views";
		String templateName = "/hello.ftl";
		String fileName = templatesPath + "/hello.html";
        FileUtil.analysisTemplate(templatesPath, templateName, fileName, root);
        return mv;  
    }  
5、直接返回模版頁面

 ModelAndView mv = new ModelAndView("hello");  
        mv.addObject("title", "Spring MVC And Freemarker");  
        mv.addObject("content", " Hello world , test my first spring mvc ! ");
 return mv;    





相關文章