Re: How to generate the output for a given java collection object
Rajvinder Pal <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Thanks Daniel! finally i am able to get the expected output with your suggesstion:-
<#list itemMap?keys as key>${key}
</#list>
I was trying to evaluate the FreeMarker for a requirement to generate the html table dynamically.I felt FreeMarker is bit tricky to solve the problem. But the mailing list response is amazing.te
with respect to generating output i am facing one more challenge it may be very silly to ask but since i am new to FreeMarker i want to know, how can we write template with proper indention but still able to get the get the output html in proper indention.
for example suppose my template logic looks like:-
for each bean
print <beean.....
<# if condition.....>
<#if condition .... >
<#if condition >
print <inner tag.......
The above logic will print something similar to this:-
<bean.....
<inner
IN the above output, indention is too much for second .
How can we have template as well generated output in proper xml type format? i mean with proper indention
<#list itemMap(key) as item>${item}</#list>
Thanks,
Rajvinder
From: Daniel Dekany <[email protected]>
To: Rajvinder Pal <[email protected]>
Cc: FreeMarker-user <[email protected]>
Sent: Saturday, September 24, 2011 1:22 PM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
The problem is that FTL, the template language, doesn't really support
maps... It only has a type that is like a Map<String, Object>, i.e.,
the keys are String-s. It's horrible. Regardless, ?keys *happens* to
work even if the map keys are non-String. But the [] operator won't.
What I always did is using BeansWraper. Where you configure FreeMarker
do this:
Configuration cfg ...
...
BeansWrapper wrapper = new BeansWrapper();
wrapper.setSimpleMapWrapper(true);
cfg.setObjectWrapper(wrapper);
Now you can use itemMap(key) to get items with non-String keys, so
your example will work. (Or, if you are using
wrapper.setSimpleMapWrapper(false), then you could just use the Java
API of the map, like itemMap.keySet() and itemMap.get(key), but then
?keys and such will give you a mess of true keys and method names, so
I don't recommend that.)
Note that wrapping XML nodes, Jython object and such won't be
automatic with this wrapper. Can be fixed with a custom one if you
care.
An beware, HashSet is unsorted. It doesn't give back its items in the
order as you have added them. (It's a Java thing, nothing to do with
FreeMarker.) Use LinkedHashSet if you need to keep the order.
--
Best regards,
Daniel Dekany
Friday, September 23, 2011, 12:54:06 PM, Rajvinder Pal wrote:
> I tried the below one. but i am getting error:-
>
> <#list itemMap?keys as key>
> ${key}
> <#list itemMap[key] as item>${item}</#list>
> </#list>
>
> error:-
> Java backtrace for programmers:
> ----------
> freemarker.template.TemplateException
> : Expected number, sequence, or string. itemMap evaluated instead
> to freemarker.template.SimpleHash on line 2, column 14 in complexMap.ftl.
> at freemarker.core.TemplateObject.invalidTypeException(
> TemplateObject.java:135)
> at freemarker.core.DynamicKeyName.dealWithNumericalKey(
> DynamicKeyName.java:125)
> at freemarker.core.DynamicKeyName._getAsTemplateModel(
> DynamicKeyName.java:90)
> at freemarker.core.Expression.getAsTemplateModel(
> Expression.java:89)
> at freemarker.core.IteratorBlock.accept(
> IteratorBlock.java:94)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.MixedContent.accept(
> MixedContent.java:92)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.IteratorBlock$Context.runLoop(
> IteratorBlock.java:167)
> at freemarker.core.Environment.visit(
> Environment.java:417)
> at freemarker.core.IteratorBlock.accept(
> IteratorBlock.java:102)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.Environment.process(
> Environment.java:190)
> at freemarker.template.Template.process(
> Template.java:237)
> at org.raj.ComplexMap.main(
> ComplexMap.java:56)
>
>
> From: Daniel Henrique Alves Lima <[email protected]>
> To: Rajvinder Pal <[email protected]>; FreeMarker-user
> <[email protected]>
> Sent: Friday, September 23, 2011 7:28 AM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> I would try something like
>
> [#list (itemMap?keys) as key]
> ${key} [#list (itemMap[key]) as item]${item}[/#list]
> [/#list]
>
> But I'm not sure if FM supports integers as hash keys.
>
> Rajvinder Pal wrote:
>
> Hi ,
>
> Following is my java object 'itemMap'.And i want to generate the output as given below
>
> Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
> //id 1
> Integer i1 = new Integer(1);
> Set<String> itemSet1 = new HashSet<String>();
> itemSet1.add("A");
> itemMap.put(i1, itemSet1);
> //id 2
> Integer i2 = new Integer(2);
> Set<String> itemSet2 = new HashSet<String>();
> itemSet2.add("B");
> itemSet2.add("C");
> itemMap.put(i2, itemSet2);
> //id 3
> Integer i3 = new Integer(3);
> Set<String> itemSet3 = new HashSet<String>();
> itemSet3.add("D");
> itemSet3.add("E");
> itemMap.put(i3, itemSet3);
> System.out.println(itemMap);
>
> Expected output:
>
> 1 A
> 2 B C
> 3 D E
>
> Could anyone let me know how can i generate this using freemarker template?
>
> Thanks,
> Rajvinder
>
>
>
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user