一个简单的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 FreeMarkerServletextends HttpServlet {
???????? public FreeMarkerServlet() {
?????????????????? super();
???????? }
????????
???????? @Override
???????? public void destroy() {
?????????????????? super.destroy();
???????? }
?
???????? public void doGet(HttpServletRequest request, HttpServletResponse response)
??????????????????????????? throws ServletException, IOException {
?????????????????? Configuration cfg=new Configuration();
?????????????????? //String path=this.getServletContext().getRealPath("bin/freemarker");
?????????????????? //cfg.setDirectoryForTemplateLoading(new File(path));
?????????????????? cfg.setServletContextForTemplateLoading(this.getServletContext(), "bin/freemarker");
?????????????????? Template tp=cfg.getTemplate("test.ftl");
?????????????????? //因為這個是一個Servlet,所以要用這個,并且由于在Content-Type中配置的字符集都是UTF-8,所以在此處要也設置成UTF-8
?????????????????? Writer out=new OutputStreamWriter(new FileOutputStream(this.getServletContext().getRealPath(".")+"/eg.html"),"utf-8");???//注意的是這里的字符集定要和自己配置的字符集是相同的,否則將出現亂碼。并且注意OutputStreamWriter轉為Writer的方法。
?????????????????? //PrintWriter out=new PrintWriter(new FileOutputStream(new File(this.getServletContext().getRealPath(".")+"/eg.html")));如果為servelet則response.getOutputStream()
?????????????????? Map root = new HashMap();
?????????????????? UserInfo u=new UserInfo();
?????????????????? u.setUname("熊師虎");
?????????????????? u.setUage(100);
?????????????????? root.put("u", u);//放入用戶的信息,在模板中可以取到用戶的信息
??????????????????
?????????????????? List strlist=new ArrayList();
?????????????????? strlist.add("aa");
?????????????????? strlist.add("bb");
?????????????????? strlist.add("cc");
?????????????????? strlist.add("dd");
?????????????????? root.put("strlist", strlist);
??????????????????
?????????????????? root.put("htag", "<h1>我是一級標題</h1>");
?
?????????????????? try {
??????????????????????????? tp.process(root, out);
?????????????????? } catch (TemplateException e) {
??????????????????????????? // TODO Auto-generated catch block
??????????????????????????? e.printStackTrace();
?????????????????? }
?????????????????? System.out.println("Successfull................");
??????????????????
????????
?????????????????? out.flush();
?????????????????? out.close();
???????? }
?
???????? public void doPost(HttpServletRequest request, HttpServletResponse response)
??????????????????????????? throws ServletException, IOException {
?
?????????????????? doGet(request, response);
???????? }
?
}
5、配置servlet的url-pattern為:FreeMarkerServlet
6、運行FreeMarkerTest或訪問FreeMarkerServlet,用以生成e.html
7、訪問e.html,即在瀏覽器中輸入:http://localhost:8080/freemarker/eg.html
?
?
?
?
總結
以上是生活随笔為你收集整理的一个简单的FreeMarker案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置DispatcherServlet
- 下一篇: 刘老师在买测温枪共花了390元你知道刘老