曾经写过一篇博文《
很轻的,Servlet + Freemarker 组合体,没有那么硬~》,不过那是基于Servlet 2.× 系列的,今天谈谈如何在Servlet 3.0 下使用Freemarker进行更快的开发方式。
Servlet 3.0的强大、简单的,摆脱以前的约束,重构类名还得需要到web.xml中手动修改,如今再也没有那么多烦恼,当然这仅仅是一个侧面而已,就已经说明了其强大。
Freemarker强大的模板化能力,据说解析速度超越JSP,让讨厌JSP+ JAVA混合体编程的人得到一种解脱,身心的。还有一点就是快速的模型填充,不需要随处可见的JAVA代码,任何角落都是。
总之:Servlet 3.0 + Freemarker, 超级轻的MVC组合,让人愉悦。
闲话短说,先来一个Servlet + JSP组合体:
TemplateTestAction.java@WebServlet("/test")
public class TemplateTestAction extends HttpServlet {
private static final long serialVersionUID = 88687687987980L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = "1687";
String title = "JSP测试";
String content = "这是JSP测试";
request.setAttribute("blog", new UserBlog(id, title, content, new Date()));
request.getRequestDispatcher("/WEB-INF/pages/template.jsp").forward(request, response);
}
}
对应JSP:
template.jsp<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.demo.UserBlog"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP + Servlet</title>
</head>
<body>
<%UserBlog blog = (UserBlog)request.getAttribute("blog"); %>
<h1>ID : <%=blog.getId() %></h1>
<h5>TITLE : <%=blog.getTitle() %></h5>
<p>DATETIME : <%=new SimpleDateFormat("yyyy-MM-dd HH:mm").format(blog.getDate()) %></p>
<div>
CONTENT : <br/>
<p><%=blog.getContent() %></p>
</div>
</body>
</html>
看看代码,以前大家也都是这些写过来的。
对比一下Servlet + Freemarker :
TemplateTest1Action.java@WebServlet("/test1")
public class TemplateTest1Action extends HttpServlet {
private static final long serialVersionUID = 6576879808909808L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = "1688";
String title = "使用freemarker";
String content = "这是测试";
request.setAttribute("blog", new UserBlog(id, title, content, new Date()));
request.getRequestDispatcher("/WEB-INF/pages/template1.html").forward(request, response);
}
}
Servlet代码没有发生什么变化,只是这次转向了html文件:
<!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=UTF-8">
<title>Freemaker模板测试1</title>
</head>
<body>
<h1>ID : ${blog.id}</h1>
<h5>TITLE : ${blog.title}</h5>
<p>DATETIME : ${blog.date?string("yyyy-MM-dd HH:mm")}</p>
<div>CONTENT : <br />
<p>${blog.content}</p>
</div>
</body>
</html>
请再次对比一下JSP 和 html文件的区别,您会选择写HTML还是JSP页面呢 ?
要想让TemplateTest1Action转向HTML页面生效,您需要配置一个Freemarker的控制器,用以解析html页面。
这里控制器为:TemplateController.java 文件:
@WebServlet(
urlPatterns = {"*.html"}, // 需要定义Freemarker解析的页面后缀类型
asyncSupported = false,
loadOnStartup = 0,
name = "templateController",
displayName = "TemplateController",
initParams = {
@WebInitParam(name = "TemplatePath", value = "/"),
@WebInitParam(name = "NoCache", value = "true"),//定义是否缓存
@WebInitParam(name = "ContentType", value = "text/html; charset=UTF-8"),// 定义内容类型
@WebInitParam(name = "template_update_delay", value = "0"), // 开发环境中可设置为0
@WebInitParam(name = "default_encoding", value = "UTF-8"),
@WebInitParam(name = "number_format", value = "0.##########")
}
)
public class TemplateController extends FreemarkerServlet {
private static final long serialVersionUID = 8714019900490761087L;
}
这里仅仅需要继承FreemarkerServlet即可,再加上一些注解即可,内容代码不用写。
当然也可以省去TemplateController,直接在web.xml文件中配置:
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>NoCache</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>ContentType</param-name>
<param-value>text/html; charset=UTF-8</param-value>
</init-param>
<init-param>
<param-name>template_update_delay</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>default_encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>number_format</param-name>
<param-value>0.##########</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
记得把 freemarker-2.3.13.jar 文件扔进WEB-INF/lib 目录下。
项目源代码下载地址:
下载地址
接下来一篇将体验一下Servlet 3.0 的WebFragment功能,支持组件、功能的插拔,使之可以模块化构造一个站点服务,大的跨越,一个变革,必将受益开发者社区。