Discussion:
[FreeMarker-user] How to Use Custom Bean in Freemarker
Dinesh Gupta
2012-11-28 16:04:47 UTC
Permalink
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;
}

}
----------------------------------
Daniel Dekany
2012-11-28 21:50:26 UTC
Permalink
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
Post by 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;
}
}
----------------------------------
Dinesh Gupta
2012-11-29 13:15:07 UTC
Permalink
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)
Post by Daniel Dekany
You just put that Users (sic) object into the data model, and then
${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
Post by 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
Post by Dinesh Gupta
----------------------------------
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);
Post by Dinesh Gupta
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)
Post by Dinesh Gupta
+ "-" + now.get(Calendar.DATE) + "-" +
now.get(Calendar.YEAR));
Post by Dinesh Gupta
// 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)
Post by Dinesh Gupta
+ "-" + 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"));
Post by Dinesh Gupta
// 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;
}
}
----------------------------------
------------------------------------------------------------------------------
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
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany
2012-11-29 20:24:15 UTC
Permalink
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
Post by 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>
-----------------
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] ----------
----------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)
You just put that Users (sic) object into the data model, and then
${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
Post by 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;
}
}
----------------------------------
------------------------------------------------------------------------------
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
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Dinesh Gupta
2012-11-30 10:58:31 UTC
Permalink
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: [***@192d342,
***@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)
Post by Daniel Dekany
So I see you expect "users" to be *list* of Users. Then, the problem
context.put("users",new Users("Diinesh","GatewayPark"));
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
Post by 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>
-----------------
FreeMarker Template example: Hello World!
======================= === County List ====
======================= 1. India 2. United States 3. Germany 4. France
======================= === User List ==== ======================= <list
user as users>
Post by Dinesh Gupta
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] ----------
----------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)
You just put that Users (sic) object into the data model, and then
${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
Post by 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
Post by Dinesh Gupta
Post by Dinesh Gupta
----------------------------------
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);
Post by Dinesh Gupta
Post by Dinesh Gupta
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)
Post by Dinesh Gupta
Post by Dinesh Gupta
+ "-" + now.get(Calendar.DATE) + "-" +
now.get(Calendar.YEAR));
Post by Dinesh Gupta
Post by Dinesh Gupta
// 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)
Post by Dinesh Gupta
Post by Dinesh Gupta
+ "-" + 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>();
Post by Dinesh Gupta
Post by Dinesh Gupta
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"));
Post by Dinesh Gupta
Post by Dinesh Gupta
// 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;
}
}
----------------------------------
------------------------------------------------------------------------------
Post by Dinesh Gupta
INSIGHTS What's next for parallel hardware, programming and related
areas?
Post by Dinesh Gupta
Interviews and blogs by thought leaders keep you ahead of the curve.
http://goparallel.sourceforge.net
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany
2012-11-30 19:08:10 UTC
Permalink
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
Post by Dinesh Gupta
Hi All,
I have given you to full source code.
Please make it runnable without error.
Thanks I advance.
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;
}
}
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>
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 ====
=======================
1.
Expression user.name is undefined on line 15, column 36 in resources/helloworld.ftl.
----------
==>> ${user.name} [on line 15, column 34 in resources/helloworld.ftl]
Post by Dinesh Gupta
----------
----------
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)
So I see you expect "users" to be *list* of Users. Then, the problem
context.put("users",new Users("Diinesh","GatewayPark"));
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
Post by 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>
-----------------
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] ----------
----------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)
You just put that Users (sic) object into the data model, and then
${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
Post by 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;
}
}
----------------------------------
------------------------------------------------------------------------------
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
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Loading...