Freemarker运用macro传递对象,及使用类的静态方法

Freemarker使用macro传递对象,及使用类的静态方法

 

两种方法:

方法一使用文件:

User

Fun

Test

test.ftl

方法二使用文件:

User

Fun

Test2

test2.ftl

方法一:

Test

public class Test {

         public static void main(String[] args) {

                     Configuration config=new Configuration ();

                     try {

                 //获取模板D:/test/test.ftl

                            config.setDirectoryForTemplateLoading (new File ("D:\\test"));

                            config.setObjectWrapper (new DefaultObjectWrapper ());

                            Template template=config.getTemplate ("test.ftl","gb18030");

 

                            // UserFun的实例放入root

                            Map root=new HashMap ();

                            Fun fun=new Fun ();

                            User user=new User ();

                            user.setAge (54);

                            user.setName ("jane");

                           

                            root.put ("fun", fun);

                            root.put ("u", user);

                           

            //输出路径

                            File file=new File ("D:\\test\\test3.html");

                            if (!file.exists ()) {

                                     System.out.println ("file not exist");

                                     file.createNewFile ();

                            }

                            Writer out=new BufferedWriter (new FileWriter (file));

                            template.process (root, out);

                            out.flush ();

                           

                   } catch (Exception e) {

                            e.printStackTrace ();

                   }

         }

}

 

User

public class User {

         private String name;

         private int age;

 

         public String getName () {

                   return name;

         }

 

         public void setName (String name) {

                   this.name = name;

         }

 

         public int getAge () {

                   return age;

         }

 

         public void setAge (int age) {

                   this.age = age;

         }

}

 

Fun

public class Fun {

         private String name;

 

         public static void print(String str) {

                   System.out.println ("Let's say:" + str);

         }

         public static User printUser (User user)

         {

                   return user;

         }

        

         public static void printUserAge (User user)

         {

                   System.err.println (user.getName () + “is” +user.getAge () +" years old");

         }

}

 

test.ftl模板

<#macro html fun u>

     <#nested/>

</#macro>

 

<@html fun=fun u=u>

   ${u.name}

<#--使用静态方法,参数为String类型-->

${fun.print ("hey")} 

<#--使用静态方法,参数为Object类型,带返回值-->

<#assign x=fun.printUser (u)>

   ${x.name}

<#--使用静态方法,参数为Object类型-->

   ${fun.printUserAge (u)}

</@html>

 

 

方法二:

Test2

public class Test 2{

         public static void main(String[] args) {

                     Configuration config=new Configuration ();

                     try {

                 //获取模板D:/test/test.ftl

                            config.setDirectoryForTemplateLoading (new File ("D:\\test"));

                            config.setObjectWrapper (new DefaultObjectWrapper

Template template=config.getTemplate ("test2.ftl","gb18030");

                            Map root=new HashMap ();

 

                            //方法一1:use ${fun.print("sss") } in ftl file

                            BeansWrapper wrapper = BeansWrapper.getDefaultInstance ();

                            TemplateHashModel staticModels = wrapper.getStaticModels ();

TemplateHashModel funStatics = (TemplateHashModel) staticModels.get ("cn.app.test2.Fun");

                            root.put ("fun", funStatics);

                            //方法二 2: ${statics ["cn.test2.Fun"].print ("sss")}

                            root.put ("statics", BeansWrapper.getDefaultInstance ().getStaticModels ());                      

            //输出路径

                            File file=new File ("D:\\test\\test3.html");

                            if (!file.exists ()) {

                                     System.out.println ("file not exist");

                                     file.createNewFile ();

                            }

                            Writer out=new BufferedWriter (new FileWriter (file));

                            template.process (root, out);

                            out.flush ();

                           

                   } catch (Exception e) {

                            e.printStackTrace ();

                   }

         }

}

 

test2.ftl模板

${fun.print("print directly") }

${statics["cn.app.test2.Fun"].print("user statics") }