当前位置:编程学习 > JAVA >>

一个简单的FreeMarker案例

1,首先:导入FreeMarker的Jar文件

2,写一个JavaBean用于封装数据

package freemarker;

 

public class UserInfo {

         private String uname=null;

         private int uage = 0;

         public String getUname() {

                   return uname;

         }

         public void setUname(String uname) {

                   this.uname = uname;

         }

         public int getUage() {

                   return uage;

         }

         public void setUage(int uage) {

                   this.uage = uage;

         }

}

3,写一个测试用的Test

package freemarker;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import freemarker.template.Configuration;

import freemarker.template.Template;

 

public class FreeMarkerTest {

 

         private Configuration cfg = null;

       

         public Configuration getConf(){

                   return cfg;

         }

       

         public void init() throws Exception {

                   cfg = new Configuration();

                   cfg.setDirectoryForTemplateLoading(new File("bin/freemarker"));//模板文件所在的位置

         }

         public static void main(String[] args) throws Exception {

                   FreeMarkerTest obj = new FreeMarkerTest();

                   obj.init();

                   Map root = new HashMap();

                   UserInfo u = new UserInfo();

                   u.setUname("熊师虎");

                   u.setUage(100);

                   List strList = new ArrayList();

                   strList.add("aa");

                   strList.add("bb");

                   strList.add("cc");

                   strList.add("dd");

                 

                   root.put("strlist", strList);

                   root.put("u", "u");//放入用户的信息,在模板中可以取到用户的信息

                   root.put("htag", "<h1>我是一级标题</h1>");

                   Template t = obj.getConf().getTemplate("Test.ftl");

                  //在WebRoot目录下构建一个test1.html

                   Writer out = new OutputStreamWriter(new FileOutputStream("WebRoot/eg.html"),"GBK");

                   t.process(root, out);//开始转化处理

                   System.out.println("Successfull.....");

         }

}

 

 

4,写一个Servlet

package control;

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import freemarker.UserInfo;

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;

 

public class FreeMarkerServlet extends HttpServlet {

         public FreeMarkerServlet() {

                   super();

         }

       

         @Override

         public void destroy() {

                   super.destroy();

         }

 

         public void doGet(HttpServletRequest request, HttpServletResponse response)

                            t

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,