实战Freemarker
前言
之前,做了一个展示型页面比较多的网站。
那时候,我还没有学习框架之类的东西,所以,所有的页面都是由JSP+Serlvet交互。
现在翻看,感觉非常的不爽与纠结,因为感觉性能不好,JSP要编译成Serlvet,然后JSP引擎用Javac把Serlvet(Java源文件)编译成相应的class文件。接下来,都是靠流输出打印到页面,所以还不如直接生成HTML页面,所以,就有了这一个教程。
需求分析
我们需要做一个展示型页面生成的系统。我做了一个简单的Demo,通过资源ID请求页面的信息,然后判断这个页面是否存在,存在则返回页面,不存在则生成这个页面,然后跳转到这个页面。
实例
Serlvet
HtmlShow.java
/**
*
* @author CHEN
* <pre>请求html页面</pre>
*/
@WebServlet("/jsp/html/show")
public class HtmlShow extends HttpServlet{
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("请求delete方法");
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("请求put方法");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int id=0;
try {
id=Integer.parseInt(req.getParameter("html_id"));
} catch (Exception e) {
System.out.println("有人在求虐");
}
String rootUrl=req.getServletContext().getRealPath("/jsp/html")+"/";
String fileName=new StringBuffer("html_"+id+".html").toString();
File file=new File(new StringBuffer(rootUrl+"/"+fileName).toString());
if(!file.exists()) {
//文件不存在,那就创建文件
String ftlName="html.ftl";
Map<String, String> root=new HashMap<String, String>();
root.put("username", "CHEN俊铭"+id);
HtmlCreater.create(rootUrl, fileName, ftlName, root);
} else {
}
//转发就是在这个同一个包的访问
//web应用的根目录
req.getRequestDispatcher("html_0.html").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
}
HtmlCreate.java
/**
*
* @author CHEN
* <pre>html页面生成器</pre>
*
*/
public class HtmlCreater {
/**
*
* @param rootUrl html文件根路径
* @param fileName 文件名
* @param ftlName jtl的文件名
* @param map 映射属性
* @return 操作结果
*/
public static boolean create(String rootUrl,String fileName,String ftlName,Map<String, String> root){
boolean flag=true;
Configuration config=new Configuration();
config.setClassForTemplateLoading(HtmlCreater.class, "/com/chen/qg/templates");从class 目录开始
config.setObjectWrapper(new DefaultObjectWrapper());
config.setEncoding(Locale.getDefault(), "utf-8");
//输出文件
FileOutputStream fileOutputStream;
try {
Template template=config.getTemplate(ftlName,"utf-8");//查找ftl模板
fileOutputStream = new FileOutputStream(rootUrl+fileName);
Writer out=new OutputStreamWriter(fileOutputStream,"utf-8");
try {
template.process(root, out);
out.flush();
out.close();
} catch (TemplateException e) {
e.printStackTrace();
flag=false;
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
flag=false;
} catch (IOException e1) {
e1.printStackTrace();
flag=false;
}
return flag;
}
}
html.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World!</title>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
<meta name = "format-detection" content="telephone =no" />
</head>
<body>
您好!${username?html}
这是一个模版页面
</body>
</html>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chen.qg</groupId>
<artifactId>my-freemarker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>my-freemarker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.9</version>
</dependency>
</dependencies>
<build>
<finalName>my-freemarker</finalName>
</build>
</project>
后续
直接就是代码了。
语法教程:链接