Re: Freemarker in Java app server, best practices
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, August 12, 2009, 1:25:07 AM, Chris wrote:
>> 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.
>>
Well, we start to go way off-topic in mail, but whatever...
> This makes sense, although in my app several different views have
> the same action. I'd have to write an action that would extract a
> view parameter from the URL (http://foo.com/myaction?myview=bar)
Usually you should not code both the action and the view into the URL.
That way the visitor could choose arbitrary combinations. But maybe
that's exactly what you want, I don't know... Still, such things
(extract those parameters, etc.) are not the duty of the individual
actions, but of the framework.
> and then dispatch the call to the appropriate view. Possible, but
> it's backwards from the way most frameworks work.
I don't think so... They used to start with the action, not with the
view. (On Java land at least.)
> JSP pages, for
> example, call the view and then the view figures out how to render
> itself.
That's the outdated (PHP-ish, I could say) way of using JSP, or if not
outdated then at least the way which is not for MVC (and FM is for
MVC). In modern frameworks you usually don't call JSP-s directly (you
don't see URL-s ending with ".jsp"). See JSP Model 2.
> It's a lot easier to just drop a page in a site and have it work
> without fiddling with a controller.
There are solutions for everything, only you have to find or implement
it... Like, if you find that many views has common actions, then maybe
you are going towards a CMS-like system. And Java CMS-es exist, some
even uses FreeMarker (like Magnolia), but surely you don't address the
templates in the URL-s directly. There are many many approaches to all
this, building dynamic Web sites. No wonder there are like infinite
number of Web Application Frameworks and CMS-es around, and according
their authors all is the best... ;) for what they did when they
created the framework. There is no FM-specific best practice that
applies to the framework architecture. Of course, FM templates should
only be used for strictly presentation-related tasks, because that's
what it's for. But FM doesn't dictate your framework architecture,
it's just a component. (Similarly JSP doesn't (strictly...) dictate a
framework architecture, although it's much more intrusive than FM.) So
find out what you want without considering FM, and if you find that
you need a template engine in your architecture, FM most certainly
won't be in your way. You don't even need to do messy hacks as forward
HTTP request to it (like you do in JSP model 2), as its functionality
is primarily available through its public Java API. And it doesn't
even expects to have a Servlet environment...
--
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