Re: Freemarker in Java app server, best practices
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Tuesday, August 11, 2009, 10:49:14 PM, Chris wrote:
> I currently use JSP, and am trying to migrate to Freemarker. In my JSP
> pages I normally have a backing Java class, one per page, and just
> instantiate it at the top of the page. I then call page.getThis() and
> .getThat() lower down in the page.
>
> I'm not quite sure how to get the same functionality in Freemarker. I
> could create a custom servlet for each .ftl page and then map one to the
> other in web.xml, but that seems like a waste. I'd rather be able to
> specify the appropriate servlet class right at the top of the ftl page,
> and then I won't have to add anything to web.xml.
>
> I looked at the example code in webapp2 in the Freemarker download,
> but it seems really convoluted
Well, whether the traditional put(key, name) approach should be used
for data-model building or rather the data-model should be a JavaBean
which maybe also represent the whole "action", etc., those are surely
disputable design questions. But other than that, it's as simple as it
can be, as far as the application-specific part is concerned (which
are GuestbookServlet.java and GuestbookEntry.java; the others are part
of the example framework, so they don't really count).
> and uses reflection, which just isn't very efficient.
What do you mean by it's not very efficient? Using reflection once per
HTTP request is of course totally negligible from performance
standpoint.
> What's the best way to do this?
It's of course completely up to the Web Application Framework how it
associates the views (templates) with the backing "action" (which is
usually represented by a class or a method)... do it on the way it
best fits your particular application. But usually it's the action
that chooses the view, and not the other way around. This is because
request URL-s are normally associated with actions (not with views),
as actions do the MVC Controlling and other application-logic
decisions (which may includes forwarding to another URL, which is
clearly pre-view-rendering stuff), so naturally they run first, and
only then will the view be rendered. Also, an action may conditionally
chooses from multiple views. But if you still want to do this on the
other way around, then you can use something like <#ftl
attributes={"action":"com.example.YourClass"}>, and then in your
framework (i.e., not on the application level) ensure that before
templates are executed this attribute is extracted with
theTemplate.getCustomAttribute("action") and processed as it should
be.
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july