Re: [FreeMarker-user] URL-encoding strings
Generates text that depends on changing data (like dynamic HTML).
Brought to you by:
revusky
From: <Isa...@ny...> - 2004-01-06 19:04:34
|
Hi. I've done it using a transformer. Perhaps the code below would help. This is the transformer: package org.frb.ny.template; import freemarker.template.TemplateTransformModel; import java.io.*; import java.util.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class URLEncode implements TemplateTransformModel{ private HttpServletResponse response; public URLEncode(HttpServletResponse response){ this.response = response; } public Writer getWriter(Writer out, Map args){ return new UrlEncoder(out); } private class UrlEncoder extends Writer{ public UrlEncoder(Writer out){ this.out = out; } public void write(char [] cbuf, int off, int len) throws IOException{ out.write(URLEncode.this.response.encodeURL(new String(cbuf, off, len))); } public void flush() throws IOException{ out.flush(); } public void close(){ } private Writer out; } } In the preTemplateProcess method of my Freemarker servlet extension, I add the following: ((SimpleHash)data).put("encodeUrl", new URLEncode(response)); For each request, the encodeUrl transformer is made available. Your templates can now have code that looks like this: <transform encodeUrl>../myServer/mypath</transform> Daniel Dekany <dd...@fr...> Sent by: fre...@li... 01/06/2004 01:53 PM Please respond to freemarker-user To: Per Cederberg <fre...@li...> cc: Subject: Re: [FreeMarker-user] URL-encoding strings Tuesday, January 6, 2004, 1:08:22 PM, Per Cederberg wrote: > Hi, > > I just ran into a problem I know I've encountered once > before with Freemarker - how to URL-encode strings. Are > there some easy way to do this? I've checked the manual > and couldn't find any, but it seems like a glaring > omission so I guess it's gotta be there somewhere... [snip] Known issue... I will add this to our FAQ. FreeMarker can't URL encode, since it doesn't know what encoding should it use for that, so it is left for the Web application framework to add a method for that. But, I wonder that maybe ISO-8859-1 URL encoding could be added... if you don't encode non-English letters, just spaces and such... > Otherwise, maybe a ?url or ?url_string built-in for > strings would be a good solution? It isn't that hard to > do URL-encoding in Java, just that it gets messy when > one has to do it in the templates. And I generally try > to avoid adding the same value twice to the model, as > it seems kind of silly. Path in MVC templates is a problematic question. Some say that they shouldn't be put into the template directly, but into the data model, since they belong to MVC C. In that case, URL encoding is naturally NOT the task of the template. OTOH, in the reality putting URL-s in the template can be a good thing, but then, why does it contain variable part (so you need dynamic URL encoding, and thus ?url)? > Thanks in advance for any help or guidance! > > Cheers, > > /Per -- Best regards, Daniel Dekany ------------------------------------------------------- This SF.net email is sponsored by: IBM Linux Tutorials. Become an expert in LINUX or just sharpen your skills. Sign up for IBM's Free Linux Tutorials. Learn everything from the bash shell to sys admin. Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click _______________________________________________ FreeMarker-user mailing list Fre...@li... https://lists.sourceforge.net/lists/listinfo/freemarker-user |