使用FreeMarker的Web Project例子
1 创建一个名为FreemarkerDemo的Web Project
2 删除index.jsp,新建index.html,index.html中的内容为:
- <html>
- <head>
- <title>Hello FreeMarker Example</title>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8">
- </head>
- <body>
- 点击下面链接看看效果:
- <hr>
- <a href="hello.do">调用Hello模板</a>
- </body>
- </html>
3 把web.xml中的内容为
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- id="WebApp_ID" version="3.1">
- <display-name>FreemarkerDemo</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </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文件,其内容为
- <html>
- <head>
- <title>hello</title>
- </head>
- <body>
- <h1>Hello ${user}!</h1>
- </body>
- </html>
3 在src下创建example包,在example包下创建Hello.Java,其内容为:
- package example;
- import java.io.*;
- import java.util.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
- import freemarker.template.*;
- public class Hello extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private Configuration cfg;
- public void init() {
- //初始化FreeMarker配置
- //创建一个Configuration实例
- cfg =new Configuration();
- //设置FreeMarker的模版文件位置
- cfg.setServletContextForTemplateLoading(getServletContext(),"templates");
- }
- public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
- //建立数据模型
- Map<String, String> map =new HashMap<String, String>();
- //放入对应数据key value
- map.put("user","Zheng");
- //取得模版文件
- Template t =cfg.getTemplate("hello.ftl");
- //开始准备生成输出
- //使用模版文件的charset作为本页面的charset
- //使用text/html MIME-type
- response.setContentType("text/html; charset=" + t.getEncoding());
- PrintWriter out = response.getWriter();
- //合并数据模型和模版,并将结果输出到out中
- try {
- t.process(map,out);//用模板来开发servlet可以只在代码里面加入动态的数据
- } catch(TemplateException e) {
- throw new ServletException("处理Template模版中出现错误", e);
- }
- }
- }
4 在web.xml中添加内容,添加完之后的全部内容为
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- id="WebApp_ID" version="3.1">
- <display-name>FreemarkerDemo</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>Hello</servlet-name>
- <servlet-class>example.Hello</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Hello</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
5 重新运行项目,并且在浏览器中输入http://localhost:8080/FreemakerDemo
点击“调用Hello模板”超链接,跳转到另一个界面
使用FreeMarker的Web Project例子的更多相关文章
- 在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 ...
- WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探
Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特别频繁的应用尚能相安无事, ...
- Freemarker 最简单的例子程序
首先导入包,freemarker.jar 下载地址: freemarker-2.3.18.tar.gz http://cdnetworks-kr-1.dl.sourceforge.net/pro ...
- 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中创 ...
- freemarker在web应用项目的使用
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现 ...
- 【maven】pom.xml报错:Cannot detect Web Project version.
新建的maven项目 报错如下: Cannot detect Web Project version. Please specify version of Web Project through &l ...
- Myeclipse中web project各种常见错误及解决方法(持续更新)
创建web project时的问题 error:Install Dynamic web Module Facet卡住 solution:把网络关掉再创建就可以 Servlet error:The se ...
- JDK和Tomcat环境变量,以及用MyEclipse新建Web Project测试Tomcat Server
[请尊重原创版权,如需引用,请注明来源及地址] 在此之前一直用的Eclipse挺顺手的,今天突然想换MyEclipse试试,不知安装MyEclipse的时候我选错了什么选项,反正JDK和Tomcat的 ...
- 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 ...
随机推荐
- 主机批量扫描工具fping,hping安装及使用
https://blog.csdn.net/weixin_39762926/article/details/79476196?utm_source=blogxgwz0 https://blog.csd ...
- 亲测能用的mysqli类,挺好用的
<?php header('content-type:text/html;charset=utf-8'); /* 掌握满足单例模式的必要条件 (1)私有的构造方法-为了防止在类外使用new关键字 ...
- hdu多校5
1002 思路:贪心显然不好贪,直接爆搜. #include<bits/stdc++.h> #define LL long long #define fi first #define se ...
- GIT的安装及git状态的变更详解
一.安装git环境 (2)Git安装 Centos: yum install -y git Ubuntu: apt-get install git Windows安装git bash软件 注意不要使用 ...
- CentOS 6.4 系统上如何安装 tomcat 8
CentOS 6.4 系统上如何安装 tomcat 8 本文将详细讲解在Linux系统上如何安装tomcat,tomcat是没有32位和64位之分的. 1.下载tomcat 首先我们肯定要先下载tom ...
- Java字符串易错方法总结
Java字符串易错方法总结 public String[] split(String regex) 和 public String[] split(String regex,int limit) li ...
- try catch finally 执行顺序面试题总结
在网上看到一些异常处理的面试题,试着总结一下,先看下面代码,把这个方法在main中进行调用打印返回结果,看看结果输出什么. public static int testBasic(){ int i = ...
- 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】
[已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和 ...
- json_decode转码无效
由于最近从原来常用的utf-8的字符转到了gbk:所以,在用json_decode的时候遇到了返回为空: 经查找发现是json_decode和json_encode只针对utf8字符串有效: 于是用到 ...
- 线段树+哈希【CF580E】Kefa and Watch
线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...