Re: Modifying data models within the template

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, July 8, 2011, 3:23:42 AM, Chris wrote:

> I implemented my own ObjectWrapper and that seems to work.
>
> Question #1 is still open, though. Does anyone know if modifying a data
> model during template processing will lead to threading problems?

I'm just answering really quickly and without reading much of the
thread... If multiple threads share the same POJO-s then it will cause
problems unless the POJO-s themselves are thread-safe and you only
share the POJO-s, not the wrappers themselves. If you just change the
model within a single thread, use vanilla BeansWrapper, that meant to
handle changes. (I pretty much never use DefaultObjectWrapper...)

As of templates with different data-models... maybe it's not
applicable to your case, but when I have used FreeMarker to generate
"components" on a page, each component was an independent FreeMarker
template with its own data-model. That I did by simply calling
Template.process for each components. And yes, some of them was nested
into other components; that should work.

-- 
Best regards,
 Daniel Dekany

> On 7/7/2011 4:29 PM, Chris wrote:
>> A bit of poking around shows that the default ObjectWrapper wraps Maps
>> in a SimpleHash, and the SimpleHash copies the map. That's the answer to
>> question #2. Is there an easy way to get it to stop doing this?
>>
>> Making a copy of every hash in the system strikes me as an unnecessary
>> performance killer.
>>
>>
>>
>>
>> On 7/7/2011 4:18 PM, Chris wrote:
>>> We need to give [#includes] and subpages their own datamodels to keep
>>> things modular. The scheme I've come up with is to create a directive
>>> that can be included at the top of each template that will add that
>>> template's data to the top-level data model.
>>>
>>> Two questions:
>>>
>>> 1. This is an explicit violation of the "data models are read-only" rule
>>> described here:
>>>
>>> http://freemarker.sourceforge.net/docs/pgui_misc_multithreading.html
>>>
>>> If a new datamodel is created for each page fetch, is this a problem?
>>> I'm running in a servlet environment. So far as I can tell, only the
>>> thread that is processing the page will ever see this data model.
>>>
>>> 2. When I use a HashMap as the main "root" data model, the scheme fails.
>>> The template says that the variables I've added to the model do not
>>> exist. The scheme *works* when I have the root HashMap point to an
>>> object, and then I replace the value of that object. There's something
>>> about adding a new key to the HashMap during template processing that
>>> the system doesn't like.
>>>
>>> Is the template processing code playing games with my HashMap, like
>>> making a copy? Why?


------------------------------------------------------------------------------
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-d2d-c2
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.