-
FEATURED COMPONENTS
First time here? Check out the FAQ!
How to integrate zk with freeMarker!
Hi Jentrees,
Please take a look at this project, they uses freemarker to genarate freemarker templates.
http://jpa2web.wiki.sourceforge.net/GenerationProcess
You might get help from their forum.
And we will appreciate that if you're willing to write an article introducing how to use freemarker to gernerate zk templates.
/robbie
Hi
Could you post your code and explain what do you wanna do ?
For instance, Do you wanna a zul file to include a Freemarker's file ?
if possible, please post your Freemarker's sample code
Hi
I have made a sample
test.ftl
<div style="color: red;"> ${message} </div>
freemarker.zul
<?page title="new page title" contentType="text/html;charset=UTF-8"?> <zk> <window title="new page title" border="normal" apply = "demo.templates.FreemarkerCtrl"> <html id="temp"></html> </window> </zk>
FreemarkerCtrl.java
public class FreemarkerCtrl extends GenericForwardComposer { Configuration cfg = new Configuration(); Html temp; public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); cfg.setServletContextForTemplateLoading((ServletContext)Sessions.getCurrent().getWebApp().getNativeContext(), "freemarker/templates"); Map root = new HashMap(); root.put("message", "Hello World!"); Template t = cfg.getTemplate("test.ftl"); Writer out = new StringWriter(); t.process(root, out); System.out.println("out: " + out); temp.setContent(out.toString()); } }
Hope this is what you looking for.~~
Hi
I made another sample for you
in web.xml, add setting
<servlet> <servlet-name>hello</servlet-name> <servlet-class>demo.templates.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping> <filter> <filter-name>zkFilter</filter-name> <filter-class>org.zkoss.zk.ui.http.DHtmlLayoutFilter</filter-class> <init-param> <param-name>extension</param-name> <param-value>html</param-value> <!-- Use XHTML components as default. --> </init-param> </filter> <filter-mapping> <filter-name>zkFilter</filter-name> <url-pattern>*.ftl</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>
HelloServlet.java
public class HelloServlet extends HttpServlet { private Configuration cfg; public void init() { cfg = new Configuration(); cfg.setServletContextForTemplateLoading(getServletContext(), "freemarker/"); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Build the data-model Map root = new HashMap(); root.put("message", "Hello World!"); Template t = cfg.getTemplate("ftlIncludeZUL.ftl"); // Prepare the HTTP response: // - Use the charset of template for the output // - Use text/html MIME-type resp.setContentType("text/html; charset=" + t.getEncoding()); Writer out = resp.getWriter(); // Merge the data-model and the template try { t.process(root, out); } catch (TemplateException e) { throw new ServletException( "Error while processing FreeMarker template", e); } } }
ftlIncludeZUL.ftl
<?xml version="1.0" encoding="UTF-8"?> <html xmlns:u="http://www.zkoss.org/2005/zul" xmlns:zk="http://www.zkoss.org/2005/zk"> <head> <title>FreeMarker Example Web Application 1</title> </head> <body> <u:button id="btn" label="Change label" onClick='btn.setLabel("Clicked button");'> </u:button> <u:include src="hello.zul" /> ${message} </body> </html>
hello.zul
<?page title="new page title" contentType="text/html;charset=UTF-8"?> <zk> <window title="A window" border="normal"> Hello from hello.zul~ </window> </zk>
When using the sample code above and connect to ftlIncludeZUL.ftl, will inclue a zul file from ftl file
Hello Experts!!
i've posted my thread before stumbling upon this. May any of you have experience and can help me solved by problem.I am also using freemarker and zk but separately on the same applicaiton post is here .
Thanks for reading it ^_^
Asked: 2008-08-15 01:37:05 +0800
Seen: 828 times
Last updated: Jan 31 '12