1 创建一个名为FreemarkerDemo的Web Project

2 删除index.jsp,新建index.html,index.html中的内容为:

  1. <html>
  2. <head>
  3. <title>Hello FreeMarker Example</title>
  4. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  5. </head>
  6. <body>
  7. 点击下面链接看看效果:
  8. <hr>
  9. <a href="hello.do">调用Hello模板</a>
  10. </body>
  11. </html>

3 把web.xml中的内容为

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. <display-name>FreemarkerDemo</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. </welcome-file-list>
  10. </web-app>

4 把FreemarkerDemo部署到tomcat服务器中

5 运行FreemarkerDemo程序

6 在浏览器中输入http://localhost:8080/FreemakerDemo

7 点击上图中的“调用Hello模板”,报错

下面要利用FreeMarker来实现这个点击事件。

1 在WebRoot下创建lib目录,将freemarker包放到lib目录下并刷新

2 在WebRoot下新建templates目录,在此目录下新建hello.ftl文件,其内容为

  1. <html>
  2. <head>
  3. <title>hello</title>
  4. </head>
  5. <body>
  6. <h1>Hello ${user}!</h1>
  7. </body>
  8. </html>

3 在src下创建example包,在example包下创建Hello.Java,其内容为:

  1. package example;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.*;
  6. import freemarker.template.*;
  7. public class Hello extends HttpServlet {
  8. private static final long serialVersionUID = 1L;
  9. private Configuration cfg;
  10. public void init() {
  11. //初始化FreeMarker配置
  12. //创建一个Configuration实例
  13. cfg =new Configuration();
  14. //设置FreeMarker的模版文件位置
  15. cfg.setServletContextForTemplateLoading(getServletContext(),"templates");
  16. }
  17. public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
  18. //建立数据模型
  19. Map<String, String> map =new HashMap<String, String>();
  20. //放入对应数据key value
  21. map.put("user","Zheng");
  22. //取得模版文件
  23. Template t =cfg.getTemplate("hello.ftl");
  24. //开始准备生成输出
  25. //使用模版文件的charset作为本页面的charset
  26. //使用text/html MIME-type
  27. response.setContentType("text/html; charset=" + t.getEncoding());
  28. PrintWriter out = response.getWriter();
  29. //合并数据模型和模版,并将结果输出到out中
  30. try {
  31. t.process(map,out);//用模板来开发servlet可以只在代码里面加入动态的数据
  32. } catch(TemplateException e) {
  33. throw new ServletException("处理Template模版中出现错误", e);
  34. }
  35. }
  36. }

4 在web.xml中添加内容,添加完之后的全部内容为

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. <display-name>FreemarkerDemo</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. </welcome-file-list>
  10. <servlet>
  11. <servlet-name>Hello</servlet-name>
  12. <servlet-class>example.Hello</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>Hello</servlet-name>
  16. <url-pattern>*.do</url-pattern>
  17. </servlet-mapping>
  18. </web-app>

5 重新运行项目,并且在浏览器中输入http://localhost:8080/FreemakerDemo

点击“调用Hello模板”超链接,跳转到另一个界面

使用FreeMarker的Web Project例子的更多相关文章

  1. 在eclipse中新建Dynamic web project时选择2.5和3.0的区别(里面涉及servlet和tomcat的问题)

    1.是指servlet的版本,是2.5的还是3.0的 servlet3.0以后支持异步 2.dynamic web module和对应的TOMCAT 版本 http://blog.sina.com.c ...

  2. WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探

    Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特别频繁的应用尚能相安无事, ...

  3. Freemarker 最简单的例子程序

    首先导入包,freemarker.jar   下载地址:  freemarker-2.3.18.tar.gz http://cdnetworks-kr-1.dl.sourceforge.net/pro ...

  4. 2.1 一个简单的Web工程例子

    一个简单的Web工程例子 开发环境: Eclipse: Neon Release (4.6.0) JDK:1.8.0_92 Tomcat:8.5.9 Maven:3.3.9 1. 在Eclipse中创 ...

  5. freemarker在web应用项目的使用

    FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现 ...

  6. 【maven】pom.xml报错:Cannot detect Web Project version.

    新建的maven项目 报错如下: Cannot detect Web Project version. Please specify version of Web Project through &l ...

  7. Myeclipse中web project各种常见错误及解决方法(持续更新)

    创建web project时的问题 error:Install Dynamic web Module Facet卡住 solution:把网络关掉再创建就可以 Servlet error:The se ...

  8. JDK和Tomcat环境变量,以及用MyEclipse新建Web Project测试Tomcat Server

    [请尊重原创版权,如需引用,请注明来源及地址] 在此之前一直用的Eclipse挺顺手的,今天突然想换MyEclipse试试,不知安装MyEclipse的时候我选错了什么选项,反正JDK和Tomcat的 ...

  9. Using Maven to generate a Java Project or Web project

    I often to generate a Java project or Web project with Eclipse tool. Well, I have no idea when I wan ...

随机推荐

  1. webpy 模版语言

    webpy框架拥有自己的模版语言Templetor Templetor具有Python的语言风格 可以直接嵌入Python代码 $code: videoIn = post.filename and p ...

  2. LOJ #6283. 数列分块入门 7-分块(区间乘法、区间加法、单点查询)

    #6283. 数列分块入门 7 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 2   题目描述 给出 ...

  3. Oracle常用常考集合

    登陆远程服务器 sqlplus scott/tiger@192.168.2.1[:port]/sid [as sysdba] 简单查询 select  table_name from user_tab ...

  4. XJTUOJ wmq的队伍(树状数组求 K 元逆序对)

    题目链接:http://oj.xjtuacm.com/problem/14/[分析]二元的逆序对应该都会求,可以用树状数组.这个题要求K元,我们可以看成二元的.我们先从后往前求二元逆序对数, 然后对于 ...

  5. Linux命令之ping

    ping [选项] destination ping命令向网络主机发送ICMP回传请求 详细描述:ping使用ICMP协议强制ECHO_REQUEST(回传请求)数据报从主机或网关获取ICMP协议的E ...

  6. Python开发基础-Day7-闭包函数和装饰器基础

    补充:全局变量声明及局部变量引用 python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 global关键字用来在函数或其 ...

  7. BestCoder Round #65 (ZYB's Premutation)

    ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  8. python 写文件write(string), writelines(list) ,读文件

    read()方法用于直接读取字节到字符串中,可以接参数给定最多读取的字节数,如果没有给定,则文件读取到末尾. readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节),然后整行,包 ...

  9. POJ2068 Nim 博弈论 dp

    http://poj.org/problem?id=2068 博弈论的动态规划,依然是根据必胜点和必输点的定义,才明白过来博弈论的dp和sg函数差不多完全是两个概念(前者包含后者),sg函数只是mex ...

  10. bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子

    1650: [Usaco2006 Dec]River Hopscotch 跳石子 Time Limit: 5 Sec  Memory Limit: 64 MB Description Every ye ...