0

How to integrate zk with freeMarker!

asked 2008-08-15 01:37:05 +0800

jentrees gravatar image jentrees
51 1

How to integrate zk with freeMarker!

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2008-08-18 11:23:24 +0800

robbiecheng gravatar image robbiecheng
1144 2
http://robbiecheng.sys-co...

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

link publish delete flag offensive edit

answered 2010-08-12 12:19:01 +0800

ramsnaps gravatar image ramsnaps
18

Has there been an article yet on ZK + Freemarker integration since 2008, if so can someone let me know please, need it really bad and that would be a deciding factor for us to move forward with ZK in our company.

link publish delete flag offensive edit

answered 2010-08-12 19:47:44 +0800

samchuang gravatar image samchuang
4084 4

updated 2010-08-12 19:48:35 +0800

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

link publish delete flag offensive edit

answered 2010-08-12 20:08:50 +0800

samchuang gravatar image samchuang
4084 4

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.~~

link publish delete flag offensive edit

answered 2010-08-13 23:50:08 +0800

ramsnaps gravatar image ramsnaps
18

Sam,

Saw wing's e-mail and realized you had responded. I actually was looking for reverse of what you just explained. I have asked Wing to fwd my e-mail that had details.

Thanks,
Ram

link publish delete flag offensive edit

answered 2010-08-16 05:58:54 +0800

samchuang gravatar image samchuang
4084 4

updated 2010-08-16 06:00:19 +0800

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

link publish delete flag offensive edit

answered 2012-01-31 16:49:30 +0800

blacksensei gravatar image blacksensei
234 2

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 ^_^

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-08-15 01:37:05 +0800

Seen: 828 times

Last updated: Jan 31 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More