Re: How to Use Custom Bean in Freemarker
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
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" <[email protected]> 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
> [email protected]
> 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