`
eric_hwp
  • 浏览: 120809 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

freemaker

 
阅读更多

FreeMaker第一步
转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广



FreeMarker是什么?

FreeMarker是一个”模板引擎”,是一个基于模板技术的生成文本输出的一个通用工具,是一个JAVA的包,是一个JAVA程序员可以使用的类库。

FreeMarker不是什么?

FreeMarker不是一个对最终用户的应用程序.,不是一个WEB应用程序框架.。

FreeMarker能做什么?

FreeMarker是设计为可以生成WEB PAGES.它是基于SERVLET遵循MVC模式的致力于降低、分离网页设计人员和程序员的耦合。

作为WEB应用框架的一个组件,FREEMARKER引擎本身并不了解HTTP或者SERVLETS,它只不过是利用模板加上数据生成文本页面而已,也可以在FreeMarker模板中使用JSP标签。

下面编写一个简单的FreeMarker例子。

1.     创建一个web工程,在类路径下加入FreeMarker的jar文件:freemarker.jar。

   Freemarker下载地址为:http://www.freemarker.org/index.html

2.     编写bean类Book,代码如下:


view plain
package com.zyg.fm.bean; 
 
import java.util.Date; 
 
public class Book { 
    private String bookId; 
    private String bookName; 
    private String author; 
    private Double price; 
    private Date publishDate; 
    public String getBookId() { 
        return bookId; 
    } 
    public void setBookId(String bookId) { 
        this.bookId = bookId; 
    } 
    public String getBookName() { 
        return bookName; 
    } 
    public void setBookName(String bookName) { 
        this.bookName = bookName; 
    } 
    public String getAuthor() { 
        return author; 
    } 
    public void setAuthor(String author) { 
        this.author = author; 
    } 
    public Double getPrice() { 
        return price; 
    } 
    public void setPrice(Double price) { 
        this.price = price; 
    } 
    public Date getPublishDate() { 
        return publishDate; 
    } 
    public void setPublishDate(Date publishDate) { 
        this.publishDate = publishDate; 
    } 


3.     编写servlet,代码如下:


view plain
package com.zyg.fm.servlet; 
 
import java.util.*; 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
import com.zyg.fm.bean.Book; 
 
import freemarker.template.*; 
 
/**
* @author 张燕广
*/ 
public class BookServlet extends HttpServlet 

    private Configuration cfg;  
     
    public void init()  
    { 
        //初始化FreeMarker配置 
        //创建一个Configuration实例 
        cfg = new Configuration(); 
        //设置FreeMarker模版文件的位置 
        cfg.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates"); 
    } 
     
    public void service(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
    {         
        //获取数据 
        List<Book> bookList = getBookList(); 
         
        Map map = new HashMap(); 
        map.put("bookList", bookList);   
        map.put("message","您好,张燕广"); 
        //取得模版文件 
        Template t = cfg.getTemplate("bookList.ftl");   
         
        // 开始准备生成输出 
        // 使用模版文件的charset作为本页面的charset 
        System.out.println(t.getEncoding()); 
        response.setContentType("text/html; charset="+t.getEncoding()); 
        Writer out = response.getWriter(); 
         
        //合并数据模型和模版,并将结果输出到out中 
        try 
        { 
            t.process(map, out); 
        }  
        catch (TemplateException e) 
        { 
            throw new ServletException("emplate模版中出现错误!", e); 
        } 
    } 
     
    /**
     * 模拟从数据库获取数据的操作
     * @return
     */ 
    private List<Book> getBookList(){ 
        List<Book> bookList = new ArrayList<Book>(); 
        Book book = null; 
        for(int i=0;i<10;i++){ 
            book = new Book(); 
            book.setBookId(String.valueOf(i+1)); 
            book.setBookName("FreeMarker学习之"+(i+1)); 
            book.setAuthor("changong"+(i+1)); 
            book.setPrice(new Double(100/(i+1))); 
            book.setPublishDate(new Date()); 
            bookList.add(book); 
        } 
        return bookList; 
    } 




4.在WEB-INF下创建存放FreeMarker文件的目录:templates,在该目录下载FreeMarker模板文件:bookList.ftl,其代码如下:


view plain
<html> 
<head> 
  <title>书籍列表</title> 
</head> 
<body> 
  <center> 
      <font color="blue"> 
        ${message}! 这是FreeMarker学习小例子!!! <br> 
      <hr> 
      <h1>     
        书籍列表 
      </h1> 
      </font> 
  </center> 
  <table border="1" width="100%" align="center"> 
  <tr align="center"> 
    <td>书号</td> 
    <td>书名</td> 
    <td>作者</td> 
    <td>价格</td> 
    <td>出版日期</td> 
  </tr> 
  <#list bookList as book> 
  <#if book_index%2==0> 
    <tr style="color:red" mce_style="color:red" align="center" id="tr_${book_index}"> 
  <#else> 
    <tr style="color:blue" mce_style="color:blue" align="center" id="tr_${book_index}"> 
  </#if> 
    <td>${book.bookId}</td> 
    <td>${book.bookName}</td> 
    <td>${book.author}</td> 
    <td>${book.price?string.currency}</td> 
    <td>${book.publishDate?string("yyyy年MM月dd日")}</td> 
   </tr> 
  </#list> 
  </table> 
</body> 
</html>  

5.修改web.xml文件,修改后的配置代码如下:



view plain
<?xml version="1.0" encoding="ISO-8859-1"?> 
 
<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"> 
 
<web-app> 
    <servlet> 
        <servlet-name>book</servlet-name> 
        <servlet-class>com.zyg.fm.servlet.BookServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>book</servlet-name> 
        <url-pattern>/book</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 


6.访问示例代码,在浏览器地址栏输入http://localhost:8080/fmq/book(fmq为配置的项目虚拟路径)

效果如下图所示:



 

分享到:
评论
1 楼 yufa11 2011-12-13  

相关推荐

Global site tag (gtag.js) - Google Analytics