Discussion:
[FreeMarker-user] Problem with Http GET Parameter and Freemarker Servlet
Thorsten Schleinzer
2004-08-23 08:45:16 UTC
Permalink
Hello,



I have a problem with a passed GET parameter:



When I open the URL



example.ftl?id=12345



And use



${id}



In the ftl I receive a "id undefined" error.



But both in the preprocessRequest and

preTemplateProcess in my FreemarkerServlet

the call

request.getParameter("id")

returns the correct value.



Any hints why it is not passed to the template?



I use Version 2.3



Thank you

Thorsten Schleinzer
Attila Szegedi
2004-08-23 09:17:34 UTC
Permalink
It is passed. Just use
${RequestParameters.id}

There's a reason for not including parameters as global variables but
instead as subvariables of "RequestParameters" - this was already
discussed on the list, see:
<http://sourceforge.net/mailarchive/forum.php?thread_id=3987560&forum_id=9968>
if you're interested.

Cheers,
Attila.
--
home: http://www.szegedi.org
Visit Szegedi Butterfly fractals at:
http://www.szegedi.org/fractals/butterfly/index.html
Daniel Dekany
2004-09-11 17:39:37 UTC
Permalink
Monday, August 23, 2004, 10:45:16 AM, Thorsten Schleinzer wrote:

What? August 23?
Post by Thorsten Schleinzer
Hello,
When I open the URL
example.ftl?id=12345
And use
${id}
In the ftl I receive a "id undefined" error.
But both in the preprocessRequest and
preTemplateProcess in my FreemarkerServlet
the call
request.getParameter("id")
returns the correct value.
Any hints why it is not passed to the template?
[snip]

FreeMarker is an MVC-ish technology, so I always thought that exposing
highly technical and cleanly MVC controller related stuff to templates
as HTTP request parameters is a bad idea. If somebody wants to follow
that style, then he can use JSP model 1 (or traditional PHP, ASP, etc.),
where you can use powerful Java language snippets. The point of "stupid
simple" page template techniques as FreeMarker, Velocity or WebMacro are
exactly that you move these technical things into Java "action" classes
or methods, and the template only deals with the displaying of the data
that the "action" has already prepared for it.

What you can access as top-level variables in templates are the
*attributes* of the HttpServletRequest, not the parameters. Maybe you
have confused these after reading pgui_misc_servlet.html. Attributes are
created by your Java code (the "action"), and not by the visitor (who
can send whatever parameters to the server).

But, of course it is possible to access the request params directly,
maybe Attila will tell if the FreemarkerServlet has such built in
feature. If it doesn't, just create a TemplateHashModel implementation
that wraps a HttpServletRequest and exposes it parameters as items in
the hash, and drop it into the data model. But I still recommend you to
read the ID parameter in the Java code, and expose its value as the
variable "id", rather than give the templates access to the request
parameters.
--
Best regards,
Daniel Dekany


____________________________________________________________________
Miert fizetsz az internetert? Korlatlan, ingyenes internet hozzaferes a FreeStarttol.
Probald ki most! http://www.freestart.hu
Attila Szegedi
2004-09-12 12:44:01 UTC
Permalink
Post by Daniel Dekany
But, of course it is possible to access the request params directly,
maybe Attila will tell if the FreemarkerServlet has such built in
feature.
Sure. Classical case of RTFM. JavaDoc for FreemarkerServlet tells you that:

"It makes all request, request parameters, session, and servlet context
attributes available to templates through Request, RequestParameters,
Session, and Application variables."

So, RequestParameters.id should work for retrieving a request parameter
named "id".

Attila.

Loading...