freemaker入门例子

news/2024/7/4 1:42:01 标签: freemarker, hashmap, templates, class, html, user
html" title=class>class="baidu_pl">
html" title=class>class="article_content clearfix">
html" title=class>class="htmledit_views">

1.把包lib/html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.jar拷贝到项目中

2.新建模板文件WEB-INF/html" title=class>class="tags" href="/tags/TEMPLATES.html" title=templates>templates/test.ftl,内容如下:

Hello,${name}!

3.新建一个操作类Class1.java,(把模板装载到jsp页面中).  内容如下

package com.abc.web;

import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.PageContext;

import html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.template.Configuration;
import html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.template.Template;

public html" title=class>class Class1 {
public void execute(PageContext pageContext)throws Exception
{
Configuration cfg =new Configuration();
cfg.setServletContextForTemplateLoading(pageContext.getServletContext(), "WEB-INF/html" title=class>class="tags" href="/tags/TEMPLATES.html" title=templates>templates");

Map root =new HashMap();
root.put("name","Tom");

Template t = cfg.getTemplate("test.ftl");

Writer out = pageContext.getResponse().getWriter();

t.process(root, out);
}
}

4.新建一个jsp页面test1.jsp,内容如下:

<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding
="GB2312"%>
<%@ pageimport="com.abc.web.Class1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Insert title here</title>
</head>
<body>
<%
Class1 c1 =new Class1();
c1.execute(pageContext);
%>
</body>
</html>

页面的显示效果为:

Hello,Tom!

html" title=class>class="cnblogs_code">  
html" title=class>class="cnblogs_code"> --------------------------------------------------------只使用一个模板页,没有使用jsp页面------------------------------------
html" title=class>class="cnblogs_code">  
html" title=class>class="cnblogs_code"> 1.把包lib/ html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.jar拷贝到项目中
html" title=class>class="cnblogs_code">

2.  在WEB-INF下新建文件夹html" title=class>class="tags" href="/tags/TEMPLATES.html" title=templates>templates  在html" title=class>class="tags" href="/tags/TEMPLATES.html" title=templates>templates下新建test.ftl文件

内容为:

<html>

html" title=class>class="cnblogs_code">  <head>      <title>Hello Word</title>     </head> 

html" title=class>class="cnblogs_code">   <body>        

html" title=class>class="cnblogs_code"><h3>${message},${name}</h3> 

html" title=class>class="cnblogs_code">    </body> 

html" title=class>class="cnblogs_code"></html

3.    新建Servlet,内容如下:

package com.njy.html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.servlet;

import java.io.IOException;
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 html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.template.Configuration;
import html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.template.Template;
import html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.template.TemplateException;

public html" title=class>class HelloFreeMarker extends HttpServlet {

 public HelloFreeMarker() {
  super();
 }

 //负责管理FreeMarker模板的Configuration实例
 private Configuration cfg = null;
 
 public void init() throws ServletException {
  //创建一个FreeMarker实例
  cfg = new Configuration();
  //指定FreeMarker模板文件的位置
  cfg.setServletContextForTemplateLoading(getServletContext(),"/WEB-INF/html" title=class>class="tags" href="/tags/TEMPLATES.html" title=templates>templates");
 }
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  doPost(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  //建立数据模型
  Map root = new HashMap();
  root.put("message", "hello world");
  root.put("name", "聂靖宇");
  root.put("personList", list);
  
  //获取模板文件
  Template t = cfg.getTemplate("test.ftl");
  
  //开始准备生成输出
  //- 使用模板文件的Charset作为本页面的charset
  //- 使用text/html MIME-type
  response.setContentType("text/html; charset=" + t.getEncoding());
  Writer out = response.getWriter();
  //合并数据模型和模板,并将结果输出到out中
  try {
   t.process(root, out); // 往模板里写数据
  } catch (TemplateException e) {
   e.printStackTrace();
  }
 }

 public void destroy() {
  super.destroy();
 }
}
4web.xml中配置servlet

<servlet>
    <servlet-name>HelloFreeMarker</servlet-name>
    <servlet-html" title=class>class>com.njy.html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarker.servlet.HelloFreeMarker</servlet-html" title=class>class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloFreeMarker</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

5.    运行http://localhost:8080/html" title=class>class="tags" href="/tags/FREEMARKER.html" title=freemarker>freemarkerTest/hello,页面效果如下如所示:

hello world,聂靖宇

小一 11 11.01 小二 12 12.02 小三 13 13.03 小四 14 14.04 小五 15 15.05 this is a Child! this is a Child! this is a Child! this is a Child! this is a Child!

-----------------------------------------------------一些相关知识点-------------------------------------------------------------

1模板+数据模型=输出

2数据模型一览

<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Welcome ${user}!</h1>
  <p>Our latest product:
  <a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html>  

 模板能用的所有数据被包装成data-model数据模型。 
(root)
  |
  +- user = "Big Joe"
  |
  +- latestProduct
      |
      +- url = "products/greenmouse.html"
      |
      +- name = "green mouse"

html" title=class>class="cnblogs_code">  
html" title=class>class="cnblogs_code">  
html" title=class>class="cnblogs_code"> 3相关标签
html" title=class>class="cnblogs_code">      Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!
html" title=class>class="cnblogs_code">
<#list whatnot.fruits as fruit>
 <li>${fruit}
</#list>
html" title=class>class="cnblogs_code">  
html" title=class>class="cnblogs_code"> <#include "/copyright_footer.html">
html" title=class>class="cnblogs_code">  
html" title=class>class="cnblogs_code"> 4内建函数
html" title=class>class="cnblogs_code"> ${'abc'?substring(0, 1)}        a
html" title=class>class="cnblogs_code"> ${"  green mouse"?cap_first}  Green mouse
html" title=class>class="cnblogs_code"> 注意如果你想安全地插入一个属性,你必须在HTML模板中使用引号标记(是",而不
是')为属性值加引号:
html" title=class>class="cnblogs_code"> <input type=text name=user value="${user?html}">
html" title=class>class="cnblogs_code">  

http://www.niftyadmin.cn/n/1424907.html

相关文章

学员面试_恭喜我们三位税务局学员面试逆袭!

截至发稿&#xff0c;已有36276人关注我们&#xff01;这是爱举重的蜗牛发布的第360篇文章面试成绩恭喜我们的三位学员&#xff01;面试全部逆袭&#xff01;三位学员都是考税务局&#xff01;其中扬州和唐山税务两位已进入体检名单第三位学员面试逆袭5分&#xff0c;但遗憾总分…

freemarker进阶

handy_framework handy框架 handy框架是基于java技术&#xff0c;学习门槛低&#xff0c;应用于WEB应用程序快速开发的MVC框架&#xff0c; 利用freemarker作为模板页面引擎&#xff0c;使用jsp或freemarker或html做为视图&#xff0c; 是一个实现了页面及代码完全分离的MVC开…

python 下载股票数据_利用python下载股票交易数据

前段时间玩Python时无意看到了获取股票交易数据的tushare模块&#xff0c;由于自己对股票交易挺有兴趣&#xff0c;加上现在又在做数据挖掘工作&#xff0c;故想先将股票数据下载到数据库中&#xff0c;以便日后分析&#xff1a;# 导入需要用到的模块from queue import Queueim…

oracle补习

http://www.dbabeta.com/2008/oracle_database_link_basics.html1.本地安装oracle 2.使用PL/SQL Developer连接后,练习常用命令 1、使用超级用户登录sqlplus。   2、创建表空间&#xff1a;   SQL> create tablespace test   2 datafile D:\oracle\oradata\…

老船履带工具使用方法_眉山小型履带车使用方法

眉山小型履带车使用方法优质不锈钢作为新型的工业汽车重要组成部分&#xff0c;经过长期的不断创新发展&#xff0c;正日益受到广大客户的喜爱&#xff0c;进而会为汽车的安全行驶提供一种更高效、更安全、更环保、更有效的方式。保护功能:防锈、防褪色、防变形。拆除及注意事项…

梦想成真

是有很多优点: 排错处理,日志记录,线程安全,多线程.线程池,

转成数组_array数组

一、数组 1. 定义数组&#xff08;array&#xff09;是按次序排列的一组值。每个值的位置都有编号&#xff08;从 0 开始&#xff09;&#xff0c;整个数组用方括号表示。var arr [a, b, c];任何类型的数据&#xff0c;都可以放入数组。如果数组的元素还是数组&#xff0c;就形…

win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程

http://www.cnblogs.com/icewee/articles/2019783.html 数据库下载链接&#xff1a;http://222.132.81.146/rj/cs_sql_2005_dev_all_dvd.rar