url encoding of java string in freemarker template file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Wai
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

url encoding of java string in freemarker template file

Wai
In a freemarker template, I have the following. Of course, it can also come from a java method.
<#assign test=Static["java.lang.String"].format("http://example.com/page?a=1&b=2") >
<p>result1=${test}</p>
<p>result2=${test?url('utf-8')}</p>

The output to the webbrowser shows...
result1=http://example.com/page?a=1&b=2
result2=http%26%23x3a%3B%26%23x2f%3B%26%23x2f%3Bexample.com%26%23x2f%3Bpage%26%23x3f%3Ba%26%23x3d%3B1%26amp%3Bb%26%23x3d%3B2

I'm interesting in the following correct output...
result1=http://example.com/page?a=1&b=2
result2=http%3A%2F%2Fexample.com%2Fpage%3Fa%3D1%26b%3D2


Could someone tell me what is the problem and how to solve it.
Thanks in advance.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: url encoding of java string in freemarker template file

taher
I'm not sure there is a problem? FreeMarker is doing exactly what
you're telling it to do by escaping any URL characters. This behavior
is well documented [1]

So I guess the question is, why and in what context are you using the
url() built-in for.

[1] http://freemarker.org/docs/ref_builtins_string.html#ref_builtin_url

On Wed, Jul 26, 2017 at 5:04 AM, Wai <[hidden email]> wrote:

> In a freemarker template, I have the following. Of course, it can also come
> from a java method.
> <#assign
> test=Static["java.lang.String"].format("http://example.com/page?a=1&b=2") >
> <p>result1=${test}</p>
> <p>result2=${test?url('utf-8')}</p>
>
> The output to the webbrowser shows...
> result1=http://example.com/page?a=1&b=2
> result2=http%26%23x3a%3B%26%23x2f%3B%26%23x2f%3Bexample.com%26%23x2f%3Bpage%26%23x3f%3Ba%26%23x3d%3B1%26amp%3Bb%26%23x3d%3B2
>
> I'm interesting in the following correct output...
> result1=http://example.com/page?a=1&b=2
> result2=http%3A%2F%2Fexample.com%2Fpage%3Fa%3D1%26b%3D2
>
>
> Could someone tell me what is the problem and how to solve it.
> Thanks in advance.
>
>
>
>
> --
> View this message in context: http://ofbiz.135035.n4.nabble.com/url-encoding-of-java-string-in-freemarker-template-file-tp4708874.html
> Sent from the OFBiz - User mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: url encoding of java string in freemarker template file

Deepak Dixit-3
You can refer UtilCodec class

requestUrl = UtilCodec.getDecoder("url").encode(requestUrl);

requestUrl = UtilCodec.getDecoder("url").decode(requestUrl);


Thanks & Regards
--
Deepak Dixit
www.hotwaxsystems.com
www.hotwax.co

On Wed, Jul 26, 2017 at 10:40 AM, Taher Alkhateeb <
[hidden email]> wrote:

> I'm not sure there is a problem? FreeMarker is doing exactly what
> you're telling it to do by escaping any URL characters. This behavior
> is well documented [1]
>
> So I guess the question is, why and in what context are you using the
> url() built-in for.
>
> [1] http://freemarker.org/docs/ref_builtins_string.html#ref_builtin_url
>
> On Wed, Jul 26, 2017 at 5:04 AM, Wai <[hidden email]> wrote:
> > In a freemarker template, I have the following. Of course, it can also
> come
> > from a java method.
> > <#assign
> > test=Static["java.lang.String"].format("http://example.com/page?a=1&b=2")
> >
> > <p>result1=${test}</p>
> > <p>result2=${test?url('utf-8')}</p>
> >
> > The output to the webbrowser shows...
> > result1=http://example.com/page?a=1&b=2
> > result2=http%26%23x3a%3B%26%23x2f%3B%26%23x2f%3Bexample.
> com%26%23x2f%3Bpage%26%23x3f%3Ba%26%23x3d%3B1%26amp%3Bb%26%23x3d%3B2
> >
> > I'm interesting in the following correct output...
> > result1=http://example.com/page?a=1&b=2
> > result2=http%3A%2F%2Fexample.com%2Fpage%3Fa%3D1%26b%3D2
> >
> >
> > Could someone tell me what is the problem and how to solve it.
> > Thanks in advance.
> >
> >
> >
> >
> > --
> > View this message in context: http://ofbiz.135035.n4.nabble.
> com/url-encoding-of-java-string-in-freemarker-template-file-tp4708874.html
> > Sent from the OFBiz - User mailing list archive at Nabble.com.
>
Wai
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: url encoding of java string in freemarker template file

Wai
In reply to this post by taher
The issue was the actual _result2_ is longer then the desired _result2_. Notice that the actual _result2_ contain many more bytes then the desired.  I think the issue is that a java string is using unicode/utf16. That's why there are so many more bytes.

A solution I used was to create a StringWrapper for the _test_ variable and then use the freemarker ?url method. eg...

<#assign test=Static["java.lang.String"].format("http://example.com/page?a=1&b=2") >
<#assign test=StringUtil.wrapString(test)>
<p>result1=${test}</p>
<p>result2=${test?url('utf-8')}</p>

Resulting output to the browser is...
result1=http://example.com/page?a=1&b=2
result2=http%3A%2F%2Fexample.com%2Fpage%3Fa%3D1%26b%3D2
Wai
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: url encoding of java string in freemarker template file

Wai
In reply to this post by Deepak Dixit-3
Thanks
Loading...