Re: (no subject)
Nancy Wang <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.devel |
|---|---|
| Message-ID | <CACZZE-sGcJ+-iyiv5KjDBrrnG6gJSiHJ1a=sxDXgHv83zLALYQ@mail.gmail.com> |
Great! Thank you very much, Daniel ! Best regards, Nancy On Fri, Feb 24, 2012 at 10:54 AM, Daniel Dekany <[email protected]> wrote: > Friday, February 24, 2012, 4:34:33 PM, Nancy Wang wrote: > > > Hi, Daniel, > > > > Thanks a lot for your advice! It works now, but the result not in > > sorted order, could it have sort order such as alphabetical order? > > Use <#list keys?sort as key> > > > ftl > > <tr> > > <#assign keys = user?keys> > > <#list keys as key> > > <#if key != "class" && !user[key]?is_method> > > <td>${key}</td> > > </#if> > > </#list> > > </tr> > > <tr> > > <#assign keys = user?keys> > > <#list keys as key> > > <#if key != "class" && !user[key]?is_method> > > <td>${user[key]}</td> > > </#if> > > </#list> > > </tr> > > > > output > > <tr> > > <td>lastName</td> > > <td>dob</td> > > <td>firstName</td> > > <td>dep</td> > > </tr> > > <tr> > > <td>Johnson</td> > > <td>19800920</td> > > <td>Byrant</td> > > <td>HR</td> > > </tr> > > > > Thanks and best regards, > > Nancy > > > > > > On Thu, Feb 23, 2012 at 4:41 PM, Daniel Dekany <[email protected]> > wrote: > > Thursday, February 23, 2012, 8:38:24 PM, Nancy Wang wrote: > > > >> Hi, Daniel, > >> > >> Thank you very much for your quick response! I tried user?keys > >> > >> > >> <table> > >> <tr> > >> <#assign keys = user?keys> > >> <#list keys as key> > >> <#if key != "class" && key != "getClass" && key != "toString" && > >> key != "equals" && key != "hashCode" && !key?starts_with("get") && > !key?starts_with("set") > > > > > Actually... ?keys is not that bad for this if I think about it more. > > Because, you should just use <#if !user[key]?is_method> instead of > > trying to checking against all possible method names (you never know > > what methods will be there). > > > >> <td>${key}</td> > >> </#if> > >> </#list> > >> </tr> > >> </table> > >> > >> Now I couldn't get the key's value. I tried user[key], > >> user.get(key), none of them work. You advice is highly appreciated! > > > > But user[key] should work. What's the error message? > > > >> Thanks and best regards, > >> Nancy > >> > >> ---------- Forwarded message ---------- > >> From: Daniel Dekany <[email protected]> > >> Date: Thu, Feb 23, 2012 at 1:26 AM > >> Subject: Re: [Freemarker-devel] (no subject) > >> To: FreeMarker-devel <[email protected]> > >> > >> > >> Unfortunately you can't do that out-of-the-box. Some may say that you > >> should use user?keys, but that will return not only the property names > >> but the method names too. But you could write a TemplatMethodModelEx > >> (that's like a #function only it's implemented in Java) that tries to > get > >> the non-wrapped Java object with > >> ((AdapterTemplateModel) arg).getAdaptedObject(Object.class) > >> then gets the property names with the java.beans API-s, builds a > >> freemarker.template.SimpleSequence based on what it sees, and returns > >> it. So you create a such TemplatMethodModelEx, let's call it > >> com.example.GetPropertyNamesMethod, and then put it into an #import-ed > >> ftl where you have > >> <#assign getPropertyNames = > >> "com.example.GetPropertyNamesMethod"?new()>. > >> > >> -- > >> Best regards, > >> Daniel Dekany > >> > >> > >> Wednesday, February 22, 2012, 10:53:40 PM, Nancy Wang wrote: > >> > >>> Hi, all, > >>> > >>> I have a ftl file, would like to know if there is a generic way to > >>> show all field values of a user and all field names (suppose I > >>> don't know what fields are there in User bean) > >>> > >>> ftl file > >>> <table> > >>> <tr> > >>> <td>${user.firstName}</td> > >>> <td>${user.lastName}</td> > >>> <td>${user.dob}</td> > >>> <td>${user.dep}</td> > >>> </tr> > >>> </table> > >>> > >>> > >>> java file > >>> Map root = new HashMap(); > >>> User user = new User(); > >>> user.setFirstName("Byrant"); > >>> user.setLastName("Johnson"); > >>> user.setDob("19800920"); > >>> user.setDep("HR"); > >>> > >>> root.put("user", user); > >>> > >>> User bean > >>> private String firstName; > >>> private String lastName; > >>> private String dob; > >>> private String dep; > >>> public String getFirstName() > >>> > >>> ..................... > > -- > Best regards, > Daniel Dekany > > > > ------------------------------------------------------------------------------ > Virtualization & Cloud Management Using Capacity Planning > Cloud computing makes use of virtualization - but cloud computing > also focuses on allowing computing to be delivered as a service. > http://www.accelacomm.com/jaw/sfnl/114/51521223/ > _______________________________________________ > FreeMarker-devel mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/freemarker-devel > ------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/ _______________________________________________ FreeMarker-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-devel