freemarker在servlet中的使用

1、在web.xml引入FreeMarker提供的Servlet



  freemarker_web
 
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
 

  
 
  freemarker
  freemarker.ext.servlet.FreemarkerServlet
    
 
 
    TemplatePath
    /
 

 
 
    NoCache
    true
 

 
 
    ContentType
    text/html;UTF-8
 

    
 
 
    template_update_delay
    0
 

 
 
    default_encoding
    UTF-8
 

 
 
    number_format
   
    0.##
 


  1




 freemarker
 *.ftl
 



2、新建test.ftl模板文件

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">  
<head>  
 <title>test!title>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
head>  
<body>  
    <div>  
        直接显示字符串<br />  
        String:${strValue}<br/>     
        <hr/>  
    div>  
      
    <div>  
    显示日期  
    date:${myDate?date}<br/>  
    <hr/>  
    显示时间  
    time:${myDate?time}<br/>  
    <hr/>  
    显示时间戳  
    datetime:${myDate?datetime}<br/>  
    <hr/>   
    div>  
      
    <div>  
    访问对象数组  
    ObjectArray:  
    ${objectArray[0]},${objectArray[1]},${objectArray[2]}  
    <br />  
    <hr/>       
    div>  
      
    <div>  
    遍历Set中的元素  
    SetData:<br />  
    <#list setData as value>  
        ${value}  
    #list>  
    <hr/>   
    div>  
      
    <div>  
    遍历List中的元素  
    strList:<br/>  
    <#list strList as strValue >  
    <#if strValue_index == 0>  
        ${strValue} is first element  
    <#elseif !strValue_has_next>  
        ${strValue} is last element  
    <#else>  
        ${strValue}  
    #if>  
    <br />  
    #list>  
    <hr/>   
    div>  
      
    <div>  
    获取map中的值  
    <br/>  
    map:  
    key1=${map.key1},key2=${map.key2},key3=${map.key3}  
    <hr/>       
    div>  
      
      
    <#include "foot.html">  
body>     
html> 

3、编写Servlet

public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public TestServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String strValue = "this is String";
        Date myDate = new Date();
        Object[] objectArray = new Object[] { 1, "str", 1.2 };

        Set setData = new HashSet();
        setData.add("dataValue1");
        setData.add("dataValue2");
        setData.add("dataValue3");

        List strList = new ArrayList();
        strList.add("字符串1");
        strList.add("字符串2");
        strList.add("字符串3");

        Map map = new HashMap();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");

        request.setAttribute("strValue", strValue);
        request.setAttribute("objectArray", objectArray);
        request.setAttribute("strList", strList);
        request.setAttribute("map", map);
        request.setAttribute("myDate", myDate);
        request.setAttribute("setData", setData);
        request.getRequestDispatcher("/WEB-INF/ftl/test.ftl").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

} 
  



你可能感兴趣的:(freemarker)