How to Use Custom Bean in Freemarker

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to Use Custom Bean in Freemarker

Dinesh Gupta
Please help me out this problem.
 How can I access the POJO(custom bean) I Freemarker.

How can I use BeansWrapper or  other DataModal. Please give me an example of the one


----------------------------------
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;



import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class Main {
        private static final Logger LOGGER = Logger.getLogger(Main.class);
        public static void main(String[] args) {
                LOGGER.info("Entering main in Main");
                Calendar now = Calendar.getInstance();
                System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
                                + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

                // add days to current date using Calendar.add method
                now.add(Calendar.DATE, 1);

                System.out.println("date after one day : "
                                + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
                                + "-" + now.get(Calendar.YEAR));
                sample();
        }
        private static void sample(){
                try {
                        LOGGER.info("Entering sample in Case Manager");
                        Configuration fmCfg = new Configuration();
                        /*BeansWrapper beanWrapper = new BeansWrapper();
                        //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
                        beanWrapper.setExposeFields(true);
                        fmCfg.setObjectWrapper(beanWrapper);*/
                        Template template = fmCfg.getTemplate("./resources/helloworld.ftl");
                        SimpleHash context = new SimpleHash(ObjectWrapper.BEANS_WRAPPER);
                       
                        // Build the data-model
                        context.put("message", "Hello World!");

                        //List parsing
                        List<String> countries = new ArrayList<String>();
                        countries.add("India");
                        countries.add("United States");
                        countries.add("Germany");
                        countries.add("France");
                        context.put("countries", countries);
                        List<Users> lUsers = new ArrayList<Users>(2);
                        lUsers.add(new Users("Diinesh","GatewayPark"));
                        lUsers.add(new Users("Sandeep","Banglore"));
                        context.put("users",new Users("Diinesh","GatewayPark"));
                       
                        // Console output
                        Writer out = new OutputStreamWriter(System.out);
                        template.process(context, out);
                        out.flush();

                        // File output
                        Writer file = new FileWriter(new File("./output/FTL_helloworld.html"));
                        template.process(context, file);
                        file.flush();
                        file.close();
                } catch (IOException e) {
                        LOGGER.error(e);
                } catch (TemplateException e) {
                        LOGGER.error(e);
                }
        }
}
class Users{
        String name;
        String place;
        public Users(String name, String place) {
                this.name = name;
                this.place = place;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getPlace() {
                return place;
        }
        public void setPlace(String place) {
                this.place = place;
        }
       
}        
----------------------------------
------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
INSIGHTS What's next for parallel hardware, programming and related areas?
Interviews and blogs by thought leaders keep you ahead of the curve.
http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Custom Bean in Freemarker

Daniel Dekany
You just put that Users (sic) object into the data model, and then
access its properties like:

  ${users.name}

This works with the default wrapper too, since that extends
BeansWrapper. (Although I would avoid the default wrapper in new
projects, and just use pure BeansWrapper... but that's another story.)

--
Best regards,
 Daniel Dekany


Wednesday, November 28, 2012, 5:04:47 PM, Dinesh Gupta wrote:

> Please help me out this problem.
>  How can I access the POJO(custom bean) I Freemarker.
> How can I use BeansWrapper or  other DataModal. Please give me an example of the one
>
>
> ----------------------------------
> import java.io.File;
> import java.io.FileWriter;
> import java.io.IOException;
> import java.io.OutputStreamWriter;
> import java.io.Writer;
> import java.util.ArrayList;
> import java.util.Calendar;
> import java.util.List;
>
> import org.apache.log4j.Logger;
>
>
>
> import freemarker.template.Configuration;
> import freemarker.template.ObjectWrapper;
> import freemarker.template.SimpleHash;
> import freemarker.template.Template;
> import freemarker.template.TemplateException;
>
>
> public class Main {
>         private static final Logger LOGGER = Logger.getLogger(Main.class);
>         public static void main(String[] args) {
>                 LOGGER.info("Entering main in Main");
>                 Calendar now = Calendar.getInstance();
>                 System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
>                                 + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>
>                 // add days to current date using Calendar.add method
>                 now.add(Calendar.DATE, 1);
>
>                 System.out.println("date after one day : "
>                                 + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
>                                 + "-" + now.get(Calendar.YEAR));
>                 sample();
>         }
>         private static void sample(){
>                 try {
>                         LOGGER.info("Entering sample in Case Manager");
>                         Configuration fmCfg = new Configuration();
>                         /*BeansWrapper beanWrapper = new BeansWrapper();
>                        
> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
>                         beanWrapper.setExposeFields(true);
>                         fmCfg.setObjectWrapper(beanWrapper);*/
>                         Template template =
> fmCfg.getTemplate("./resources/helloworld.ftl");
>                         SimpleHash context = new
> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>                        
>                         // Build the data-model
>                         context.put("message", "Hello World!");
>
>                         //List parsing
>                         List<String> countries = new ArrayList<String>();
>                         countries.add("India");
>                         countries.add("United States");
>                         countries.add("Germany");
>                         countries.add("France");
>                         context.put("countries", countries);
>                         List<Users> lUsers = new ArrayList<Users>(2);
>                         lUsers.add(new Users("Diinesh","GatewayPark"));
>                         lUsers.add(new Users("Sandeep","Banglore"));
>                         context.put("users",new Users("Diinesh","GatewayPark"));
>                        
>                         // Console output
>                         Writer out = new OutputStreamWriter(System.out);
>                         template.process(context, out);
>                         out.flush();
>
>                         // File output
>                         Writer file = new FileWriter(new
> File("./output/FTL_helloworld.html"));
>                         template.process(context, file);
>                         file.flush();
>                         file.close();
>                 } catch (IOException e) {
>                         LOGGER.error(e);
>                 } catch (TemplateException e) {
>                         LOGGER.error(e);
>                 }
>         }
> }
> class Users{
>         String name;
>         String place;
>         public Users(String name, String place) {
>                 this.name = name;
>                 this.place = place;
>         }
>         public String getName() {
>                 return name;
>         }
>         public void setName(String name) {
>                 this.name = name;
>         }
>         public String getPlace() {
>                 return place;
>         }
>         public void setPlace(String place) {
>                 this.place = place;
>         }
>        
> }        
> ----------------------------------


------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
INSIGHTS What's next for parallel hardware, programming and related areas?
Interviews and blogs by thought leaders keep you ahead of the curve.
http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Custom Bean in Freemarker

Dinesh Gupta

Dear Daniel,
Thanks for quick replying.
Below template I Am using with previous mention mail code
------------
: FreeMarker Template example: ${message}

======================= === County List ==== ======================= <#list countries as country> ${country_index + 1}. ${country} </#list> ${users.name} ======================= === User List ==== ======================= <list user as users> <#foreach user in users> ${user_index + 1}. ${users.name} </#foreach>
-----------------

But I am getting below error:

FreeMarker Template example: Hello World!

======================= === County List ==== ======================= 1. India 2. United States 3. Germany 4. France

======================= === User List ==== ======================= <list user as users>

Expected collection or sequence. users evaluated instead to freemarker.ext.beans.StringModel on line 15, column 27 in resources/helloworld.ftl. The problematic instruction: ----------==> foreach user in users [on line 15, column 9 in resources/helloworld.ftl] ----------

Java backtrace for programmers: ----------freemarker.template.TemplateException: Expected collection or sequence. users evaluated instead to freemarker.ext.beans.StringModel on line 15, column 27 in resources/helloworld.ftl. at freemarker.core.TemplateObject. invalidTypeException(TemplateObject.java:136) at freemarker.core.IteratorBlock$ Context.runLoop(IteratorBlock.java:190) at freemarker.core.Environment. visit(Environment.java:428) at freemarker.core.IteratorBlock. accept(IteratorBlock.java:102) at freemarker.core.Environment. visit(Environment.java:221) at freemarker.core.MixedContent. accept(MixedContent.java:92) at freemarker.core.Environment. visit(Environment.java:221) at freemarker.core.Environment. process(Environment.java:199) at freemarker.template.Template. process(Template.java:259) at com.tatainfotech.Common.Main. sample(Main.java:76) at com.tatainfotech.Common.Main.main(Main.java:46)

On Nov 29, 2012 3:21 AM, "Daniel Dekany" <[hidden email]> wrote:
You just put that Users (sic) object into the data model, and then
access its properties like:

  ${users.name}

This works with the default wrapper too, since that extends
BeansWrapper. (Although I would avoid the default wrapper in new
projects, and just use pure BeansWrapper... but that's another story.)

--
Best regards,
 Daniel Dekany


Wednesday, November 28, 2012, 5:04:47 PM, Dinesh Gupta wrote:

> Please help me out this problem.
>  How can I access the POJO(custom bean) I Freemarker.
> How can I use BeansWrapper or  other DataModal. Please give me an example of the one
>
>
> ----------------------------------
> import java.io.File;
> import java.io.FileWriter;
> import java.io.IOException;
> import java.io.OutputStreamWriter;
> import java.io.Writer;
> import java.util.ArrayList;
> import java.util.Calendar;
> import java.util.List;
>
> import org.apache.log4j.Logger;
>
>
>
> import freemarker.template.Configuration;
> import freemarker.template.ObjectWrapper;
> import freemarker.template.SimpleHash;
> import freemarker.template.Template;
> import freemarker.template.TemplateException;
>
>
> public class Main {
>         private static final Logger LOGGER = Logger.getLogger(Main.class);
>         public static void main(String[] args) {
>                 LOGGER.info("Entering main in Main");
>                 Calendar now = Calendar.getInstance();
>                 System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
>                                 + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>
>                 // add days to current date using Calendar.add method
>                 now.add(Calendar.DATE, 1);
>
>                 System.out.println("date after one day : "
>                                 + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
>                                 + "-" + now.get(Calendar.YEAR));
>                 sample();
>         }
>         private static void sample(){
>                 try {
>                         LOGGER.info("Entering sample in Case Manager");
>                         Configuration fmCfg = new Configuration();
>                         /*BeansWrapper beanWrapper = new BeansWrapper();
>
> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
>                         beanWrapper.setExposeFields(true);
>                         fmCfg.setObjectWrapper(beanWrapper);*/
>                         Template template =
> fmCfg.getTemplate("./resources/helloworld.ftl");
>                         SimpleHash context = new
> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>
>                         // Build the data-model
>                         context.put("message", "Hello World!");
>
>                         //List parsing
>                         List<String> countries = new ArrayList<String>();
>                         countries.add("India");
>                         countries.add("United States");
>                         countries.add("Germany");
>                         countries.add("France");
>                         context.put("countries", countries);
>                         List<Users> lUsers = new ArrayList<Users>(2);
>                         lUsers.add(new Users("Diinesh","GatewayPark"));
>                         lUsers.add(new Users("Sandeep","Banglore"));
>                         context.put("users",new Users("Diinesh","GatewayPark"));
>
>                         // Console output
>                         Writer out = new OutputStreamWriter(System.out);
>                         template.process(context, out);
>                         out.flush();
>
>                         // File output
>                         Writer file = new FileWriter(new
> File("./output/FTL_helloworld.html"));
>                         template.process(context, file);
>                         file.flush();
>                         file.close();
>                 } catch (IOException e) {
>                         LOGGER.error(e);
>                 } catch (TemplateException e) {
>                         LOGGER.error(e);
>                 }
>         }
> }
> class Users{
>         String name;
>         String place;
>         public Users(String name, String place) {
>                 this.name = name;
>                 this.place = place;
>         }
>         public String getName() {
>                 return name;
>         }
>         public void setName(String name) {
>                 this.name = name;
>         }
>         public String getPlace() {
>                 return place;
>         }
>         public void setPlace(String place) {
>                 this.place = place;
>         }
>
> }
> ----------------------------------


------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
INSIGHTS What's next for parallel hardware, programming and related areas?
Interviews and blogs by thought leaders keep you ahead of the curve.
http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user

------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
VERIFY Test and improve your parallel project with help from experts
and peers. http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Custom Bean in Freemarker

Daniel Dekany
So I see you expect "users" to be *list* of Users. Then, the problem
is this line:

  context.put("users",new Users("Diinesh","GatewayPark"));

You wanted to write this:

  context.put("users", lUsers);

Also inside the #foreach you want ${user.name}, not ${users.name}.

(Also, why's the class name "Users"? It should be User, as it
represents a *single* user.)

--
Best regards,
 Daniel Dekany


Thursday, November 29, 2012, 2:15:07 PM, Dinesh Gupta wrote:

> Dear Daniel,
> Thanks for quick replying.
> Below template I Am using with previous mention mail code
> ------------
> : FreeMarker Template example: ${message}
> ======================= === County List ====
> ======================= <#list countries as country> ${country_index
> + 1}. ${country} </#list> ${users.name} ======================= ===
> User List ==== ======================= <list user as users>
> <#foreach user in users> ${user_index + 1}. ${users.name} </#foreach>
> -----------------
> But I am getting below error:
> FreeMarker Template example: Hello World!
> ======================= === County List ====
> ======================= 1. India 2. United States 3. Germany 4. France
> ======================= === User List ==== ======================= <list user as users>
> Expected collection or sequence. users evaluated instead to
> freemarker.ext.beans.StringModel on line 15, column 27 in
> resources/helloworld.ftl. The problematic instruction: ----------==>
> foreach user in users [on line 15, column 9 in
> resources/helloworld.ftl] ----------
> Java backtrace for programmers:
> ----------freemarker.template.TemplateException: Expected collection
> or sequence. users evaluated instead to
> freemarker.ext.beans.StringModel on line 15, column 27 in
> resources/helloworld.ftl. at freemarker.core.TemplateObject.
> invalidTypeException(TemplateObject.java:136) at
> freemarker.core.IteratorBlock$
> Context.runLoop(IteratorBlock.java:190) at
> freemarker.core.Environment. visit(Environment.java:428) at
> freemarker.core.IteratorBlock. accept(IteratorBlock.java:102) at
> freemarker.core.Environment. visit(Environment.java:221) at
> freemarker.core.MixedContent. accept(MixedContent.java:92) at
> freemarker.core.Environment. visit(Environment.java:221) at
> freemarker.core.Environment. process(Environment.java:199) at
> freemarker.template.Template. process(Template.java:259) at
> com.tatainfotech.Common.Main. sample(Main.java:76) at
> com.tatainfotech.Common.Main.main(Main.java:46)
> On Nov 29, 2012 3:21 AM, "Daniel Dekany" <[hidden email]> wrote:
> You just put that Users (sic) object into the data model, and then
> access its properties like:
>
>   ${users.name}
>
> This works with the default wrapper too, since that extends
> BeansWrapper. (Although I would avoid the default wrapper in new
> projects, and just use pure BeansWrapper... but that's another story.)
>
> --
> Best regards,
>  Daniel Dekany
>
>
> Wednesday, November 28, 2012, 5:04:47 PM, Dinesh Gupta wrote:
>
>> Please help me out this problem.
>>  How can I access the POJO(custom bean) I Freemarker.
>> How can I use BeansWrapper or  other DataModal. Please give me an example of the one
>>
>>
>> ----------------------------------
>> import java.io.File;
>> import java.io.FileWriter;
>> import java.io.IOException;
>> import java.io.OutputStreamWriter;
>> import java.io.Writer;
>> import java.util.ArrayList;
>> import java.util.Calendar;
>> import java.util.List;
>>
>> import org.apache.log4j.Logger;
>>
>>
>>
>> import freemarker.template.Configuration;
>> import freemarker.template.ObjectWrapper;
>> import freemarker.template.SimpleHash;
>> import freemarker.template.Template;
>> import freemarker.template.TemplateException;
>>
>>
>> public class Main {
>>         private static final Logger LOGGER = Logger.getLogger(Main.class);
>>         public static void main(String[] args) {
>>                 LOGGER.info("Entering main in Main");
>>                 Calendar now = Calendar.getInstance();
>>                 System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
>>                                 + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>>
>>                 // add days to current date using Calendar.add method
>>                 now.add(Calendar.DATE, 1);
>>
>>                 System.out.println("date after one day : "
>>                                 + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
>>                                 + "-" + now.get(Calendar.YEAR));
>>                 sample();
>>         }
>>         private static void sample(){
>>                 try {
>>                         LOGGER.info("Entering sample in Case Manager");
>>                         Configuration fmCfg = new Configuration();
>>                         /*BeansWrapper beanWrapper = new BeansWrapper();
>>
>> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
>>                         beanWrapper.setExposeFields(true);
>>                         fmCfg.setObjectWrapper(beanWrapper);*/
>>                         Template template =
>> fmCfg.getTemplate("./resources/helloworld.ftl");
>>                         SimpleHash context = new
>> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>>
>>                         // Build the data-model
>>                         context.put("message", "Hello World!");
>>
>>                         //List parsing
>>                         List<String> countries = new ArrayList<String>();
>>                         countries.add("India");
>>                         countries.add("United States");
>>                         countries.add("Germany");
>>                         countries.add("France");
>>                         context.put("countries", countries);
>>                         List<Users> lUsers = new ArrayList<Users>(2);
>>                         lUsers.add(new Users("Diinesh","GatewayPark"));
>>                         lUsers.add(new Users("Sandeep","Banglore"));
>>                         context.put("users",new Users("Diinesh","GatewayPark"));
>>
>>                         // Console output
>>                         Writer out = new OutputStreamWriter(System.out);
>>                         template.process(context, out);
>>                         out.flush();
>>
>>                         // File output
>>                         Writer file = new FileWriter(new
>> File("./output/FTL_helloworld.html"));
>>                         template.process(context, file);
>>                         file.flush();
>>                         file.close();
>>                 } catch (IOException e) {
>>                         LOGGER.error(e);
>>                 } catch (TemplateException e) {
>>                         LOGGER.error(e);
>>                 }
>>         }
>> }
>> class Users{
>>         String name;
>>         String place;
>>         public Users(String name, String place) {
>>                 this.name = name;
>>                 this.place = place;
>>         }
>>         public String getName() {
>>                 return name;
>>         }
>>         public void setName(String name) {
>>                 this.name = name;
>>         }
>>         public String getPlace() {
>>                 return place;
>>         }
>>         public void setPlace(String place) {
>>                 this.place = place;
>>         }
>>
>> }
>> ----------------------------------
>
>
> ------------------------------------------------------------------------------
> Keep yourself connected to Go Parallel:
> INSIGHTS What's next for parallel hardware, programming and related areas?
> Interviews and blogs by thought leaders keep you ahead of the curve.
> http://goparallel.sourceforge.net
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
VERIFY Test and improve your parallel project with help from experts
and peers. http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Custom Bean in Freemarker

Dinesh Gupta


Hi All,
I have given you to full source code.
Please make it runnable without error.

Thanks I advance.

Code:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

import org.apache.log4j.Logger;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Main {
        private static final Logger LOGGER = Logger.getLogger(Main.class);
        public static void main(String[] args) {
                LOGGER.info("Entering main in Main");
                Calendar now = Calendar.getInstance();
                System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
                                + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

                // add days to current date using Calendar.add method
                now.add(Calendar.DATE, 1);

                System.out.println("date after one day : "
                                + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
                                + "-" + now.get(Calendar.YEAR));
                sample();
        }
        private static void sample(){
                try {
                        LOGGER.info("Entering sample in Case Manager");
                        Configuration fmCfg = new Configuration();
                        /*BeansWrapper beanWrapper = new BeansWrapper();
                        //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
                        beanWrapper.setExposeFields(true);
                        fmCfg.setObjectWrapper(beanWrapper);*/
                        Template template = fmCfg.getTemplate("./resources/helloworld.ftl");
                        SimpleHash context = new SimpleHash(ObjectWrapper.BEANS_WRAPPER);
                       
                        // Build the data-model
                        context.put("message", "Hello World!");

                        //List parsing
                        List<String> countries = new ArrayList<String>();
                        countries.add("India");
                        countries.add("United States");
                        countries.add("Germany");
                        countries.add("France");
                        context.put("countries", countries);
                        List<User> lUsers = new ArrayList<User>(2);
                        lUsers.add(new User("Dinesh","GatewayPark"));
                        lUsers.add(new User("Sandeep","Banglore"));
                        context.put("users",lUsers);
                       
                        // Console output
                        Writer out = new OutputStreamWriter(System.out);
                        template.process(context, out);
                        out.flush();

                        // File output
                        Writer file = new FileWriter(new File("./output/FTL_helloworld.html"));
                        template.process(context, file);
                        file.flush();
                        file.close();
                } catch (IOException e) {
                        LOGGER.error(e);
                } catch (TemplateException e) {
                        LOGGER.error(e);
                }
        }
}
class User{
        String name;
        String place;
        public User(String name, String place) {
                this.name = name;
                this.place = place;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getPlace() {
                return place;
        }
        public void setPlace(String place) {
                this.place = place;
        }
}

Template :
        FreeMarker Template example: ${message}
         
        =======================
        ===  County List   ====
        =======================
        <#list countries as country>
            ${country_index + 1}. ${country}
        </#list>
       
        =======================
        ===  User List   ====
        =======================
        User Objects: ${users}
        <#list users as user>
                ${user_index+1}. ${user.name}
        </#list>
Error I am getting:

        Current date : 11-30-2012
date after one day : 12-1-2012
FreeMarker Template example: Hello World!
         
        =======================
        ===  County List   ====
        =======================
            1. India
            2. United States
            3. Germany
            4. France
       
        =======================
        ===  User List   ====
        =======================
        User Objects: [com.tatainfotech.Common.User@192d342, com.tatainfotech.Common.User@6b97fd]
                1.
Expression user.name is undefined on line 15, column 36 in resources/helloworld.ftl.
The problematic instruction:
----------
==> ${user.name} [on line 15, column 34 in resources/helloworld.ftl]
----------

Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression user.name is undefined on line 15, column 36 in resources/helloworld.ftl.
        at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
        at freemarker.core.Expression.getStringValue(Expression.java:118)
        at freemarker.core.Expression.getStringValue(Expression.java:93)
        at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
        at freemarker.core.Environment.visit(Environment.java:428)
        at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.Environment.process(Environment.java:199)
        at freemarker.template.Template.process(Template.java:259)
        at com.tatainfotech.Common.Main.sample(Main.java:76)
        at com.tatainfotech.Common.Main.main(Main.java:46)

On Nov 30, 2012 1:54 AM, "Daniel Dekany" <[hidden email]> wrote:
So I see you expect "users" to be *list* of Users. Then, the problem
is this line:

  context.put("users",new Users("Diinesh","GatewayPark"));

You wanted to write this:

  context.put("users", lUsers);

Also inside the #foreach you want ${user.name}, not ${users.name}.

(Also, why's the class name "Users"? It should be User, as it
represents a *single* user.)

--
Best regards,
 Daniel Dekany


Thursday, November 29, 2012, 2:15:07 PM, Dinesh Gupta wrote:

> Dear Daniel,
> Thanks for quick replying.
> Below template I Am using with previous mention mail code
> ------------
> : FreeMarker Template example: ${message}
> ======================= === County List ====
> ======================= <#list countries as country> ${country_index
> + 1}. ${country} </#list> ${users.name} ======================= ===
> User List ==== ======================= <list user as users>
> <#foreach user in users> ${user_index + 1}. ${users.name} </#foreach>
> -----------------
> But I am getting below error:
> FreeMarker Template example: Hello World!
> ======================= === County List ====
> ======================= 1. India 2. United States 3. Germany 4. France
> ======================= === User List ==== ======================= <list user as users>
> Expected collection or sequence. users evaluated instead to
> freemarker.ext.beans.StringModel on line 15, column 27 in
> resources/helloworld.ftl. The problematic instruction: ----------==>
> foreach user in users [on line 15, column 9 in
> resources/helloworld.ftl] ----------
> Java backtrace for programmers:
> ----------freemarker.template.TemplateException: Expected collection
> or sequence. users evaluated instead to
> freemarker.ext.beans.StringModel on line 15, column 27 in
> resources/helloworld.ftl. at freemarker.core.TemplateObject.
> invalidTypeException(TemplateObject.java:136) at
> freemarker.core.IteratorBlock$
> Context.runLoop(IteratorBlock.java:190) at
> freemarker.core.Environment. visit(Environment.java:428) at
> freemarker.core.IteratorBlock. accept(IteratorBlock.java:102) at
> freemarker.core.Environment. visit(Environment.java:221) at
> freemarker.core.MixedContent. accept(MixedContent.java:92) at
> freemarker.core.Environment. visit(Environment.java:221) at
> freemarker.core.Environment. process(Environment.java:199) at
> freemarker.template.Template. process(Template.java:259) at
> com.tatainfotech.Common.Main. sample(Main.java:76) at
> com.tatainfotech.Common.Main.main(Main.java:46)
> On Nov 29, 2012 3:21 AM, "Daniel Dekany" <[hidden email]> wrote:
> You just put that Users (sic) object into the data model, and then
> access its properties like:
>
>   ${users.name}
>
> This works with the default wrapper too, since that extends
> BeansWrapper. (Although I would avoid the default wrapper in new
> projects, and just use pure BeansWrapper... but that's another story.)
>
> --
> Best regards,
>  Daniel Dekany
>
>
> Wednesday, November 28, 2012, 5:04:47 PM, Dinesh Gupta wrote:
>
>> Please help me out this problem.
>>  How can I access the POJO(custom bean) I Freemarker.
>> How can I use BeansWrapper or  other DataModal. Please give me an example of the one
>>
>>
>> ----------------------------------
>> import java.io.File;
>> import java.io.FileWriter;
>> import java.io.IOException;
>> import java.io.OutputStreamWriter;
>> import java.io.Writer;
>> import java.util.ArrayList;
>> import java.util.Calendar;
>> import java.util.List;
>>
>> import org.apache.log4j.Logger;
>>
>>
>>
>> import freemarker.template.Configuration;
>> import freemarker.template.ObjectWrapper;
>> import freemarker.template.SimpleHash;
>> import freemarker.template.Template;
>> import freemarker.template.TemplateException;
>>
>>
>> public class Main {
>>         private static final Logger LOGGER = Logger.getLogger(Main.class);
>>         public static void main(String[] args) {
>>                 LOGGER.info("Entering main in Main");
>>                 Calendar now = Calendar.getInstance();
>>                 System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
>>                                 + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>>
>>                 // add days to current date using Calendar.add method
>>                 now.add(Calendar.DATE, 1);
>>
>>                 System.out.println("date after one day : "
>>                                 + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
>>                                 + "-" + now.get(Calendar.YEAR));
>>                 sample();
>>         }
>>         private static void sample(){
>>                 try {
>>                         LOGGER.info("Entering sample in Case Manager");
>>                         Configuration fmCfg = new Configuration();
>>                         /*BeansWrapper beanWrapper = new BeansWrapper();
>>
>> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
>>                         beanWrapper.setExposeFields(true);
>>                         fmCfg.setObjectWrapper(beanWrapper);*/
>>                         Template template =
>> fmCfg.getTemplate("./resources/helloworld.ftl");
>>                         SimpleHash context = new
>> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>>
>>                         // Build the data-model
>>                         context.put("message", "Hello World!");
>>
>>                         //List parsing
>>                         List<String> countries = new ArrayList<String>();
>>                         countries.add("India");
>>                         countries.add("United States");
>>                         countries.add("Germany");
>>                         countries.add("France");
>>                         context.put("countries", countries);
>>                         List<Users> lUsers = new ArrayList<Users>(2);
>>                         lUsers.add(new Users("Diinesh","GatewayPark"));
>>                         lUsers.add(new Users("Sandeep","Banglore"));
>>                         context.put("users",new Users("Diinesh","GatewayPark"));
>>
>>                         // Console output
>>                         Writer out = new OutputStreamWriter(System.out);
>>                         template.process(context, out);
>>                         out.flush();
>>
>>                         // File output
>>                         Writer file = new FileWriter(new
>> File("./output/FTL_helloworld.html"));
>>                         template.process(context, file);
>>                         file.flush();
>>                         file.close();
>>                 } catch (IOException e) {
>>                         LOGGER.error(e);
>>                 } catch (TemplateException e) {
>>                         LOGGER.error(e);
>>                 }
>>         }
>> }
>> class Users{
>>         String name;
>>         String place;
>>         public Users(String name, String place) {
>>                 this.name = name;
>>                 this.place = place;
>>         }
>>         public String getName() {
>>                 return name;
>>         }
>>         public void setName(String name) {
>>                 this.name = name;
>>         }
>>         public String getPlace() {
>>                 return place;
>>         }
>>         public void setPlace(String place) {
>>                 this.place = place;
>>         }
>>
>> }
>> ----------------------------------
>
>
> ------------------------------------------------------------------------------
> Keep yourself connected to Go Parallel:
> INSIGHTS What's next for parallel hardware, programming and related areas?
> Interviews and blogs by thought leaders keep you ahead of the curve.
> http://goparallel.sourceforge.net
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
TUNE You got it built. Now make it sing. Tune shows you how.
http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Custom Bean in Freemarker

Daniel Dekany
One problem that I can spot is that the User class should be public. I
believe this is a JavaBean rule, and FreeMarker relies on the JavaBean
facility of the Java platform.

--
Best regards,
 Daniel Dekany


Friday, November 30, 2012, 11:58:31 AM, Dinesh Gupta wrote:

> Hi All,
> I have given you to full source code.
> Please make it runnable without error.
> Thanks I advance.
> Code:
> import java.io.File;
> import java.io.FileWriter;
> import java.io.IOException;
> import java.io.Writer;
> import java.util.ArrayList;
> import java.util.Calendar;
> import java.util.Collection;
> import java.util.Hashtable;
> import java.util.List;
> import org.apache.log4j.Logger;
> import freemarker.ext.beans.BeansWrapper;
> import freemarker.template.Configuration;
> import freemarker.template.ObjectWrapper;
> import freemarker.template.SimpleHash;
> import freemarker.template.Template;
> import freemarker.template.TemplateException;
> public class Main {
>         private static final Logger LOGGER = Logger.getLogger(Main.class);
>         public static void main(String[] args) {
>                 LOGGER.info("Entering main in Main");
>                 Calendar now = Calendar.getInstance();
>                 System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
>                                 + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>                 // add days to current date using Calendar.add method
>                 now.add(Calendar.DATE, 1);
>                 System.out.println("date after one day : "
>                                 + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
>                                 + "-" + now.get(Calendar.YEAR));
>                 sample();
>         }
>         private static void sample(){
>                 try {
>                         LOGGER.info("Entering sample in Case Manager");
>                         Configuration fmCfg = new Configuration();
>                         /*BeansWrapper beanWrapper = new BeansWrapper();
>                        
> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
>                         beanWrapper.setExposeFields(true);
>                         fmCfg.setObjectWrapper(beanWrapper);*/
>                         Template template =
> fmCfg.getTemplate("./resources/helloworld.ftl");
>                         SimpleHash context = new
> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>                        
>                         // Build the data-model
>                         context.put("message", "Hello World!");
>                         //List parsing
>                         List<String> countries = new ArrayList<String>();
>                         countries.add("India");
>                         countries.add("United States");
>                         countries.add("Germany");
>                         countries.add("France");
>                         context.put("countries", countries);
>                         List<User> lUsers = new ArrayList<User>(2);
>                         lUsers.add(new User("Dinesh","GatewayPark"));
>                         lUsers.add(new User("Sandeep","Banglore"));
>                         context.put("users",lUsers);
>                        
>                         // Console output
>                         Writer out = new OutputStreamWriter(System.out);
>                         template.process(context, out);
>                         out.flush();
>                         // File output
>                         Writer file = new FileWriter(new
> File("./output/FTL_helloworld.html"));
>                         template.process(context, file);
>                         file.flush();
>                         file.close();
>                 } catch (IOException e) {
>                         LOGGER.error(e);
>                 } catch (TemplateException e) {
>                         LOGGER.error(e);
>                 }
>         }
> }
> class User{
>         String name;
>         String place;
>         public User(String name, String place) {
>                 this.name = name;
>                 this.place = place;
>         }
>         public String getName() {
>                 return name;
>         }
>         public void setName(String name) {
>                 this.name = name;
>         }
>         public String getPlace() {
>                 return place;
>         }
>         public void setPlace(String place) {
>                 this.place = place;
>         }
> }
> Template :
>         FreeMarker Template example: ${message}
>          
>         =======================
>         ===  County List   ====
>         =======================
>         <#list countries as country>
>             ${country_index + 1}. ${country}
>         </#list>
>        
>         =======================
>         ===  User List   ====
>         =======================
>         User Objects: ${users}
>         <#list users as user>
>                 ${user_index+1}. ${user.name}
>         </#list>
> Error I am getting:
>         Current date : 11-30-2012
> date after one day : 12-1-2012
> FreeMarker Template example: Hello World!
>          
>         =======================
>         ===  County List   ====
>         =======================
>             1. India
>             2. United States
>             3. Germany
>             4. France
>        
>         =======================
>         ===  User List   ====
>         =======================
>         User Objects: [com.tatainfotech.Common.User@192d342,
> com.tatainfotech.Common.User@6b97fd]
>                 1.
> Expression user.name is undefined on line 15, column 36 in resources/helloworld.ftl.
> The problematic instruction:
> ----------
==>> ${user.name} [on line 15, column 34 in resources/helloworld.ftl]

> ----------
> Java backtrace for programmers:
> ----------
> freemarker.core.InvalidReferenceException: Expression user.name is
> undefined on line 15, column 36 in resources/helloworld.ftl.
>         at
> freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
>         at
> freemarker.core.Expression.getStringValue(Expression.java:118)
>         at
> freemarker.core.Expression.getStringValue(Expression.java:93)
>         at
> freemarker.core.DollarVariable.accept(DollarVariable.java:76)
>         at freemarker.core.Environment.visit(Environment.java:221)
>         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>         at freemarker.core.Environment.visit(Environment.java:221)
>         at
> freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
>         at freemarker.core.Environment.visit(Environment.java:428)
>         at
> freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
>         at freemarker.core.Environment.visit(Environment.java:221)
>         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>         at freemarker.core.Environment.visit(Environment.java:221)
>         at freemarker.core.Environment.process(Environment.java:199)
>         at freemarker.template.Template.process(Template.java:259)
>         at com.tatainfotech.Common.Main.sample(Main.java:76)
>         at com.tatainfotech.Common.Main.main(Main.java:46)
> On Nov 30, 2012 1:54 AM, "Daniel Dekany" <[hidden email]> wrote:
> So I see you expect "users" to be *list* of Users. Then, the problem
> is this line:
>
>   context.put("users",new Users("Diinesh","GatewayPark"));
>
> You wanted to write this:
>
>   context.put("users", lUsers);
>
> Also inside the #foreach you want ${user.name}, not ${users.name}.
>
> (Also, why's the class name "Users"? It should be User, as it
> represents a *single* user.)
>
> --
> Best regards,
>  Daniel Dekany
>
>
> Thursday, November 29, 2012, 2:15:07 PM, Dinesh Gupta wrote:
>
>> Dear Daniel,
>> Thanks for quick replying.
>> Below template I Am using with previous mention mail code
>> ------------
>> : FreeMarker Template example: ${message}
>> ======================= === County List ====
>> ======================= <#list countries as country> ${country_index
>> + 1}. ${country} </#list> ${users.name} ======================= ===
>> User List ==== ======================= <list user as users>
>> <#foreach user in users> ${user_index + 1}. ${users.name} </#foreach>
>> -----------------
>> But I am getting below error:
>> FreeMarker Template example: Hello World!
>> ======================= === County List ====
>> ======================= 1. India 2. United States 3. Germany 4. France
>> ======================= === User List ==== ======================= <list user as users>
>> Expected collection or sequence. users evaluated instead to
>> freemarker.ext.beans.StringModel on line 15, column 27 in
>> resources/helloworld.ftl. The problematic instruction: ----------==>
>> foreach user in users [on line 15, column 9 in
>> resources/helloworld.ftl] ----------
>> Java backtrace for programmers:
>> ----------freemarker.template.TemplateException: Expected collection
>> or sequence. users evaluated instead to
>> freemarker.ext.beans.StringModel on line 15, column 27 in
>> resources/helloworld.ftl. at freemarker.core.TemplateObject.
>> invalidTypeException(TemplateObject.java:136) at
>> freemarker.core.IteratorBlock$
>> Context.runLoop(IteratorBlock.java:190) at
>> freemarker.core.Environment. visit(Environment.java:428) at
>> freemarker.core.IteratorBlock. accept(IteratorBlock.java:102) at
>> freemarker.core.Environment. visit(Environment.java:221) at
>> freemarker.core.MixedContent. accept(MixedContent.java:92) at
>> freemarker.core.Environment. visit(Environment.java:221) at
>> freemarker.core.Environment. process(Environment.java:199) at
>> freemarker.template.Template. process(Template.java:259) at
>> com.tatainfotech.Common.Main. sample(Main.java:76) at
>> com.tatainfotech.Common.Main.main(Main.java:46)
>> On Nov 29, 2012 3:21 AM, "Daniel Dekany" <[hidden email]> wrote:
>> You just put that Users (sic) object into the data model, and then
>> access its properties like:
>>
>>   ${users.name}
>>
>> This works with the default wrapper too, since that extends
>> BeansWrapper. (Although I would avoid the default wrapper in new
>> projects, and just use pure BeansWrapper... but that's another story.)
>>
>> --
>> Best regards,
>>  Daniel Dekany
>>
>>
>> Wednesday, November 28, 2012, 5:04:47 PM, Dinesh Gupta wrote:
>>
>>> Please help me out this problem.
>>>  How can I access the POJO(custom bean) I Freemarker.
>>> How can I use BeansWrapper or  other DataModal. Please give me an example of the one
>>>
>>>
>>> ----------------------------------
>>> import java.io.File;
>>> import java.io.FileWriter;
>>> import java.io.IOException;
>>> import java.io.OutputStreamWriter;
>>> import java.io.Writer;
>>> import java.util.ArrayList;
>>> import java.util.Calendar;
>>> import java.util.List;
>>>
>>> import org.apache.log4j.Logger;
>>>
>>>
>>>
>>> import freemarker.template.Configuration;
>>> import freemarker.template.ObjectWrapper;
>>> import freemarker.template.SimpleHash;
>>> import freemarker.template.Template;
>>> import freemarker.template.TemplateException;
>>>
>>>
>>> public class Main {
>>>         private static final Logger LOGGER = Logger.getLogger(Main.class);
>>>         public static void main(String[] args) {
>>>                 LOGGER.info("Entering main in Main");
>>>                 Calendar now = Calendar.getInstance();
>>>                 System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
>>>                                 + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>>>
>>>                 // add days to current date using Calendar.add method
>>>                 now.add(Calendar.DATE, 1);
>>>
>>>                 System.out.println("date after one day : "
>>>                                 + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
>>>                                 + "-" + now.get(Calendar.YEAR));
>>>                 sample();
>>>         }
>>>         private static void sample(){
>>>                 try {
>>>                         LOGGER.info("Entering sample in Case Manager");
>>>                         Configuration fmCfg = new Configuration();
>>>                         /*BeansWrapper beanWrapper = new BeansWrapper();
>>>
>>> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
>>>                         beanWrapper.setExposeFields(true);
>>>                         fmCfg.setObjectWrapper(beanWrapper);*/
>>>                         Template template =
>>> fmCfg.getTemplate("./resources/helloworld.ftl");
>>>                         SimpleHash context = new
>>> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>>>
>>>                         // Build the data-model
>>>                         context.put("message", "Hello World!");
>>>
>>>                         //List parsing
>>>                         List<String> countries = new ArrayList<String>();
>>>                         countries.add("India");
>>>                         countries.add("United States");
>>>                         countries.add("Germany");
>>>                         countries.add("France");
>>>                         context.put("countries", countries);
>>>                         List<Users> lUsers = new ArrayList<Users>(2);
>>>                         lUsers.add(new Users("Diinesh","GatewayPark"));
>>>                         lUsers.add(new Users("Sandeep","Banglore"));
>>>                         context.put("users",new Users("Diinesh","GatewayPark"));
>>>
>>>                         // Console output
>>>                         Writer out = new OutputStreamWriter(System.out);
>>>                         template.process(context, out);
>>>                         out.flush();
>>>
>>>                         // File output
>>>                         Writer file = new FileWriter(new
>>> File("./output/FTL_helloworld.html"));
>>>                         template.process(context, file);
>>>                         file.flush();
>>>                         file.close();
>>>                 } catch (IOException e) {
>>>                         LOGGER.error(e);
>>>                 } catch (TemplateException e) {
>>>                         LOGGER.error(e);
>>>                 }
>>>         }
>>> }
>>> class Users{
>>>         String name;
>>>         String place;
>>>         public Users(String name, String place) {
>>>                 this.name = name;
>>>                 this.place = place;
>>>         }
>>>         public String getName() {
>>>                 return name;
>>>         }
>>>         public void setName(String name) {
>>>                 this.name = name;
>>>         }
>>>         public String getPlace() {
>>>                 return place;
>>>         }
>>>         public void setPlace(String place) {
>>>                 this.place = place;
>>>         }
>>>
>>> }
>>> ----------------------------------
>>
>>
>> ------------------------------------------------------------------------------
>> Keep yourself connected to Go Parallel:
>> INSIGHTS What's next for parallel hardware, programming and related areas?
>> Interviews and blogs by thought leaders keep you ahead of the curve.
>> http://goparallel.sourceforge.net
>> _______________________________________________
>> FreeMarker-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>


------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
TUNE You got it built. Now make it sing. Tune shows you how.
http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user