You are viewing a plain text version of this content. The canonical link for it is here.
Posted to adffaces-dev@incubator.apache.org by Martin Koci <Ma...@aura.cz> on 2006/09/03 11:11:29 UTC

ADFFACES-152 - proper method for encoding resources

Hello, 

I don't understand how to encode src attributes on HTML elements.  There
are two methods:
ExternalContext.encodeResourceURL and ViewHandler.getResourceURL. 

MyFaces button renderer uses both:
String src =
context.getApplication().getViewHandler().getResourceURL(context,
imageSrc); 
writer.writeURIAttribute("src",
context.getExternalContext().encodeResourceURL(src), "image"); 

This solutions works (in my app) and handles both situation - relative
paths and absolute paths starting with "/".

Taken from RI javadoc: NavigationHandler.getResourceURL:
Return a URL suitable for rendering (after optional encoding perfomed by
the encodeResourceURL() method of ExternalContext) that selects the
specifed web application resource. If the specified path starts with a
slash, it must be treated as context relative; otherwise, it must be
treated as relative to the action URL of the current view.

This method handles context relative paths with "/", Sun RI 1.2 uses
this method too, code from ButtonRenderer:

private String src(FacesContext context, String value) {
        if (value == null) {
            return "";
        }
        value = context.getApplication().getViewHandler().
            getResourceURL(context, value);
        return (context.getExternalContext().encodeResourceURL(value));
    }

I seems than calling ExternalContext.encodeResourceURL in renderers is
not enough.

Regards,

Martin


Re: ADFFACES-152 - proper method for encoding resources

Posted by Adam Winer <aw...@gmail.com>.
The Trinidad source code does the "/" encoding on its
own, without calling the ViewHandler - see the toURL()
method in CoreRenderer.  We actually do a bit more than
others - "//" gets turned to "/", which is very helpful
since the default strategy gives you no way to output
a server-relative URL (only page-relative, context-relative,
and absolute)!

So, Trinidad should call ExternalContext.encodeResourceURL()
and only that method.

Frankly, I haven't the faintest idea why we (the JSF EG) added
ViewHandler.getResourceURL()!  Perhaps I've forgotten the
salient points, but ExternalContext has always seemed
sufficient.

-- Adam



On 9/3/06, Martin Koci <Ma...@aura.cz> wrote:
>
> Hello,
>
> I don't understand how to encode src attributes on HTML elements.  There
> are two methods:
> ExternalContext.encodeResourceURL and ViewHandler.getResourceURL.
>
> MyFaces button renderer uses both:
> String src =
> context.getApplication().getViewHandler().getResourceURL(context,
> imageSrc);
> writer.writeURIAttribute("src",
> context.getExternalContext().encodeResourceURL(src), "image");
>
> This solutions works (in my app) and handles both situation - relative
> paths and absolute paths starting with "/".
>
> Taken from RI javadoc: NavigationHandler.getResourceURL:
> Return a URL suitable for rendering (after optional encoding perfomed by
> the encodeResourceURL() method of ExternalContext) that selects the
> specifed web application resource. If the specified path starts with a
> slash, it must be treated as context relative; otherwise, it must be
> treated as relative to the action URL of the current view.
>
> This method handles context relative paths with "/", Sun RI 1.2 uses
> this method too, code from ButtonRenderer:
>
> private String src(FacesContext context, String value) {
>         if (value == null) {
>             return "";
>         }
>         value = context.getApplication().getViewHandler().
>             getResourceURL(context, value);
>         return (context.getExternalContext().encodeResourceURL(value));
>     }
>
> I seems than calling ExternalContext.encodeResourceURL in renderers is
> not enough.
>
> Regards,
>
> Martin
>
>