TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
Servlet的轻巧高效,freemarker的强大简便,两者结合将是超轻的组合,即可避免丑陋的java代码和HTML代码杂揉,又可高效基于模板的站点开发。闲话少说,项目需要: freemarker-2.3.13.jar 定义两个Servlet: HelloAction.java 对应 /hello,借助Freemarker硬编码输出 public class HelloAction extends HttpServlet {
private static final long serialVersionUID = -6082007726831320176L; private Configuration configuration;
public void init() throws ServletException {
configuration = new Configuration();
configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/pages");
configuration.setEncoding(Locale.CHINA, "UTF-8");
} @SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 填充数据类型
Map map = new HashMap();
map.put("userName", "小敏");
Template template = configuration.getTemplate("hello.html");
response.setContentType("text/html; charset=" + template.getEncoding());
Writer out = response.getWriter();
try{
template.process(map, out);
}catch (TemplateException e) {
e.printStackTrace();
}
} public void destroy() {
super.destroy();
if(configuration != null){
configuration = null;
}
}
} 对应模板: <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用Freemarker渲染2</title>
</head>
<body>
你好, $ {userName!} !
</body>
</html> HiAction.java 对应 /hi ,借助Freemrker Servlet的拦截功能,如以往写代码方式,感觉不到Freemarker的存在。 public class HiAction extends HttpServlet {
private static final long serialVersionUID = 518767483952153077L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setAttribute("thename", "小敏");
request.getRequestDispatcher("/WEB-INF/pages/hi.html").forward(request, response);
}
} 对应的模板: <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用Freemarker渲染</title>
</head>
<body>
hi $ {thename!}~<br />
</body>
</html> 但需要在web.xml 配置文件中定义如下: <servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>
freemarker.ext.servlet.FreemarkerServlet
</servlet-class> <!-- FreemarkerServlet settings: -->
<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>
<!-- Forces UTF-8 output encoding! -->
</init-param> <!-- FreeMarker settings: -->
<init-param>
<param-name>template_update_delay</param-name>
<param-value>0</param-value><!-- 0 is for development only! Use higher value otherwise. -->
</init-param>
<init-param>
<param-name>default_encoding</param-name>
<param-value>UTF-8</param-value><!-- The encoding of the template files. -->
</init-param>
<init-param>
<param-name>number_format</param-name>
<param-value>0.##########</param-value>
</init-param> <load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping> 使用哪一种组合方式,看您喜好了。 借助于Freemarker自身的Servlet工具,只是用于拦截Servlet中forward转向使用到的HTML资源文件。 很简陋,但凑合着能看。
源码下载:http://file.javaxxz.com/2014/10/10/055929500.zip |
|