http://park.geocities.jp/gdfsm000/java/java24.html
FreeMarkerの一番最初のサンプルです。
netbeans6.1でテストしてみましたが、test.ftlはutf-8で保存されるのですが、
それだと実行したときにtest.ftlに書いた日本語(朝、昼、夜)が文字化けします。
別のエディタで、shift_jisに変更して保存するとすべて正常に表示されます。
配列を使用した例
test.ftl
<html>
<head>
<title>FreeMarker Example Web Application 1</title>
<meta http-equiv="Content-type" content="text/html; charset=shift_JIS">
</head>
<body>
${message}
<br><br>
<table>
<tr><td>朝</td><td>昼</td><td>夜</td></tr>
<#list ar as val>
<tr>
<#list val as v>
<td>${v}</td>
</#list><br>
</tr>
</#list>
</body>
</html>
HelloServlet.java
package example;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import freemarker.template.*;
import java.lang.reflect.Array;
public class HelloServlet extends HttpServlet {
private Configuration cfg;
public void init() {
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(
getServletContext(), "WEB-INF/classes/templates");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Build the data-model
Map root = new HashMap();
root.put("message", "おはようございます");
String[] ar = {"おはようございます","こんにちは","こんばんは"};
String[] br = {"goog morning","good arternoon","Goodevening"};
ArrayList cr = new ArrayList();
cr.add(ar);
cr.add(br);
root.put("ar", cr);
Template t = cfg.getTemplate("test.ftl");
resp.setContentType("text/html; charset=" + t.getEncoding());
Writer out = resp.getWriter();
try {
t.process(root, out);
} catch (TemplateException e) {
throw new ServletException(
"Error while processing FreeMarker template", e);
}
}
}
MAPを使用した例
test2.ftl
<html>
<head>
<title>FreeMarker Example Web Application 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=MS932">
</head>
<body>
${message}
<hr>
<table border=1>
<tr><th>朝</th><th>昼</th><th>夜</th>
<#list ar as value>
<tr><td>${value.a}<td>${value.b}<td>${value.c!"<br>"}</tr>
</#list>
</table>
<br><br>
これはテストの表示です
</body>
</html>
HelloServlet2.java
package example;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import freemarker.template.*;
public class HelloServlet2 extends HttpServlet {
private Configuration cfg;
public void init() {
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(
getServletContext(), "WEB-INF/classes/templates");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Map root = new HashMap();
root.put("message", "Hello World!");
HashMap ar = new HashMap();
HashMap br = new HashMap();
ar.put("a", "おはようございます");
ar.put("b", "こんにちは");
ar.put("c", "こんばんは");
br.put("a", "Good Morning");
br.put("b", "Good Afternoon");
Object[] cr = {ar, br};
root.put("ar", cr);
Template t = cfg.getTemplate("test2.ftl");
resp.setContentType("text/html; charset=" + t.getEncoding());
Writer out = resp.getWriter();
try {
t.process(root, out);
} catch (TemplateException e) {
throw new ServletException(
"Error while processing FreeMarker template", e);
}
}
}
댓글 없음:
댓글 쓰기