The guest book is a little confusing because it uses it's own model which
not everyone will want to bother with. Here's a quick example of how to do
a table.
Here's the HTML:
<html>
<head>
<title>Hello World Servlet</title>
</head>
<body bgcolor="#FFFFFF">
<p>
<table>
<list helloList as hello>
<tr>
<td>${hello.message}
<td>${hello.line}
</tr>
</list>
</table>
</body>
</html>
Here's the code:
public class HelloServlet extends HttpServlet {
private Template template;
public void init(ServletConfig config) throws ServletException {
super.init(config);
String templatePath =
getServletContext().getRealPath("/hello-world.html");
template = new Template(templatePath);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Make a template data model.
SimpleHash modelRoot = new SimpleHash();
SimpleList list = new SimpleList();
modelRoot.put("helloList", list);
for (int i = 0; i < 10; i++) {
SimpleHash hello = new SimpleHash();
hello.put("message", "hello");
hello.put("line", i);
list.add(hello);
}
// Process the template.
template.process(modelRoot, out);
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
To explain. First you create a root. This is a basically a hashtable
(SimpleHash). Next you create a list (SimpleList). Then you create a
SimpleHash for each element you want in the list.
Basically you have three building blocks to play with:
SimpleScalar - This is effectively a single value variable.
SimpleHash - This is a hashtable of key value pairs. The value
part can actually be a value or a another list or hash.
SimpleList - This defines a list of values, hashes or sublists.
Hope this helps
-- Glen
Peter <[EMAIL PROTECTED]> on 12/08/99 11:02:02
Please respond to "A mailing list for discussion about Sun Microsystem's
Java Servlet API Technology."
<[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc: (bcc: Glen Stampoultzis/ITD/MEL/Ansett/AU)
Subject: Re: out.println("...");? "Freemarker easy? Any easy to understand
examples?"
> The difference being that freemarker has lots of nice extra's and has
what
I found to be
>excellent documentation.
Really ? I have to disagree with you there. I've read the Guestbook
example,
but frankly, its way over my head!! (read : newbie :) ) I hope
www.freemarker.org will post a simpler straightforward example (sans the
HelloWorld of course)
Do you know of any web sites out there that have any sample source code on
how to output a table resultset using freemarker ? I've tried searching,
but
couldn't find any.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
_____________________________________________________________________
CAUTION - This message may contain privileged and confidential
information intended only for the use of the addressee named above.
If you are not the intended recipient of this message you are hereby
notified that any use, dissemination, distribution or reproduction
of this message is prohibited. If you have received this message in
error please notify Ansett Australia immediately. Any views expressed
in this message are those of the individual sender and may not
necessarily reflect the views of Ansett Australia.
_____________________________________________________________________
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html