即使世界明天毁灭,我也要在今天种下我的葡萄树。
posts - 112, comments - 14, trackbacks - 0, articles - 11

在Spring中如何使用Freemarker

Posted on 2006-12-20 15:18 阅读(778) 评论(0)  编辑  收藏 所属分类: XML Design
在Spring中如何使用Freemarker
  1. Copy freemarker.jar (from the lib directory of the FreeMarker distribution) into the WEB-INF/lib directory of your Web application.
  2. Insert the following section to the WEB-INF/web.xml file of your Web application (and adjust it if required):

<servlet>

  <servlet-name>freemarker</servlet-name>

  <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>

   

  <!-- FreemarkerServlet settings: -->

  <init-param>

    <param-name>TemplatePath</param-name>

    <param-value>/</param-value>

  </init-param>

  <init-param>

    <param-name>NoCache</param-name>

    <param-value>true</param-value>

  </init-param>

  <init-param>

    <param-name>ContentType</param-name>

    <param-value>text/html</param-value>

  </init-param>

   

  <!-- FreeMarker settings: -->

  <init-param>

    <param-name>template_update_delay</param-name>

    <param-value>0</param-value> <!-- 0 is for development only! Use higher value otherwise. -->

  </init-param>

  <init-param>

    <param-name>default_encoding</param-name>

    <param-value>ISO-8859-1</param-value>

  </init-param>

  <init-param>

    <param-name>number_format</param-name>

    <param-value>0.##########</param-value>

  </init-param>

 

  <load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

  <servlet-name>freemarker</servlet-name>

  <url-pattern>*.ftl</url-pattern>

</servlet-mapping> 

That's all. After this, you can use FTL files ( *.ftl ) in the same manner as JSP ( *.jsp ) files. (Of course you can choose another extension besides ftl ; it is just the convention)

Note

How does it work? Let's examine how JSP-s work. Many servlet container handles JSP-s with a servlet that is mapped to the *.jsp request URL pattern. That servlet will receive all requests where the request URL ends with .jsp , find the JSP file based on the request URL, and internally compiles it to a Servlet , and then call the generated servlet to generate the page. The FreemarkerServlet mapped here to the *.ftl URL pattern does the same, except that FTL files are not compiled to Servlet -s, but to Template objects, and then the process method of Template will be called to generate the page.

For example, instead of this JSP file (note that it heavily uses Struts tag-libs to save designers from embedded Java monsters):

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

 

<html>

<head><title>Acmee Products International</title>

<body>

  <h1>Hello <bean:write name="user"/>!</h1>

  <p>These are our latest offers:

  <ul>

    <logic:iterate name="latestProducts" id="prod">

      <li><bean:write name="prod" property="name"/>

        for <bean:write name="prod" property="price"/> Credits.

    </logic:iterate>

  </ul>

</body>

</html> 

you can use this FTL file (use ftl file extension instead of jsp ):

<html>

<head><title>Acmee Products International</title>

<body>

  <h1>Hello ${user}!</h1>

  <p>These are our latest offers:

  <ul>

    <#list latestProducts as prod>

      <li>${prod.name} for ${prod.price} Credits.

    </#list>

  </ul>

</body>

</html> 


只有注册用户登录后才能发表评论。


网站导航: