Re: best practice to create a java object instance

"Chris Lu" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
After several days, I found the easiest way to create any java object
instance is:

Inside
protected boolean preTemplateProcess(HttpServletRequest request,
HttpServletResponse response, Template template, TemplateModel data) throws
ServletException, IOException {
        ((SimpleHash) data).put("new", new
freemarker.template.utility.ObjectConstructor());
    }

Then in the FTL code, you can
<#assign aList = new("java.util.ArrayList", 100)>

Cheers!

-- 
Chris Lu
-------------------------
Instant Scalable Full-Text Search On Any Database/Application
site: http://www.dbsight.net
demo: http://search.dbsight.com
Lucene Database Search in 3 minutes:
http://wiki.dbsight.com/index.php?title=Create_Lucene_Database_Search_in_3_minutes
DBSight customer, a shopping comparison site, (anonymous per request) got
2.6 Million Euro funding!


On Fri, May 9, 2008 at 5:59 PM, Chris Lu <[email protected]> wrote:

> Er.. Not really. We actually are very MVC oriented.
>
> Basically the application does the search via Struts actions, and search
> results are rendered via Velocity or FreeMarker or JSP.
> The application provides a UI for the customers to customize the search
> results.
> So the application control the model part. And customer control the view
> part.
> It's really separating the model and the view.
>
> But not all things can be done vie FreeMarker. For example, some special
> rendering, or date format, or re-organize the order of results.
> These logic belongs to the view.
>
> Now I guess I will have to create some utility objects that can load and
> initialize a java object via reflection.
>
> --
> Chris Lu
> -------------------------
> Instant Scalable Full-Text Search On Any Database/Application
> site: http://www.dbsight.net
> demo: http://search.dbsight.com
> Lucene Database Search in 3 minutes:
> http://wiki.dbsight.com/index.php?title=Create_Lucene_Database_Search_in_3_minutes
> DBSight customer, a shopping comparison site, (anonymous per request) got
> 2.6 Million Euro funding!
>
> On Fri, May 9, 2008 at 5:04 PM, Denis <[email protected]> wrote:
>
>> Hi Chris
>>
>> > I am trying to convert the product I am working on from Velocity to
>> > Freemarker, after feeling the limitation of Velocity. One question
>> > here:
>> >
>> > What's the best practicen to create a java object instance?
>> > I see this doesn't work since it's only applicable to "TemplateModel".
>> >   <#assign hashmap = "java.util.HashMap"?new() >
>> >   <#assign obj = "com.any.java.object"?new() >
>>
>> Er, why do you want to do that in the template?
>> Certainly that belongs to the model.
>>
>> -- Denis.
>>
>> >
>> >
>> > Thanks!
>> >
>> > --
>> > Chris Lu
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
>> Don't miss this year's exciting event. There's still time to save $100.
>> Use priority code J8TL2D2.
>>
>> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
>> _______________________________________________
>> FreeMarker-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>>
>
>
>
>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.