You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Bill Lear <ra...@zopyra.com> on 2003/04/13 20:12:31 UTC

External Assets (Images)

I'm not sure that I am using the ExternalAsset class correctly.  I get
the results that I want, but it seems that I had to go through a lot
of work to get it.

I have a customer image that I need to display on a customer's page.
The customer's image is stored under the context directory, in the
images subdirectory, and the URL is stored in a database.  When a
customer logs in, they are presented with their "home" page, which has
their customer image on it.  At some point, I would like to support
images served by Apache, thus not under the context directory.

The way I go about this now, is (Home.html):


    <img jwcid="@Image" image="ognl:customerSplash"
         src="images/acmeSplash.jpg" alt="Acme Home Page"
	 width="350" height="320"/>


and (Home.java):


    private static Location iLocation;

    // look up in database ...
    private String getCustomerSplashImage() {
        return "images/acmeSplash.jpg"; // just for example purposes ...
    }

    public IAsset getCustomerSplash() {
        if (iLocation == null) {
            ServletContext context =
                getRequestCycle().getRequestContext().getServlet().getServletContext();
            iLocation = new Location(new ContextResourceLocation(context,
                                                                 "/"));
        }

        return new ExternalAsset(getCustomerSplashImage(), iLocation);
    }


But this seems exceedingly tedious.  I found no example code using
the ExternalAsset class, and am very unsure that this is the way to
do it.  


What I really want is something like this:

    private String getCustomerSplashImage() {
        return "http://www.acme.com/images/acmeSplash.jpg";
    }

    public IAsset getCustomerSplash() {
        return ExternalImageAsset(getCustomerSplashImage());
    }


Can anyone offer any wisdom?

Thanks.


Bill

RE: External Assets (Images)

Posted by "Howard M. Lewis Ship" <hl...@attbi.com>.
The Image component is for presenting create an <img> tag whose src
references an asset.  Its advantage is that the location of the asset
(private, external, context) is immaterial.

In your case, you are always using context assets, localization is not an
issue, and the location of the asset is easy to determine using stricly
lexical rules.  In your situation, I would use an Any component to generate
the "img" element, and use a short OGNL expression to construct the src
attribute.

<img jwcid="@Any" element="tr" src="ognl:customerSplashImage" size=.... />

--
Howard M. Lewis Ship
Creator, Tapestry: Java Web Components
http://jakarta.apache.org/tapestry



> -----Original Message-----
> From: Bill Lear [mailto:rael@zopyra.com] 
> Sent: Sunday, April 13, 2003 2:13 PM
> To: tapestry-user@jakarta.apache.org
> Subject: External Assets (Images)
> 
> 
> I'm not sure that I am using the ExternalAsset class 
> correctly.  I get the results that I want, but it seems that 
> I had to go through a lot of work to get it.
> 
> I have a customer image that I need to display on a 
> customer's page. The customer's image is stored under the 
> context directory, in the images subdirectory, and the URL is 
> stored in a database.  When a customer logs in, they are 
> presented with their "home" page, which has their customer 
> image on it.  At some point, I would like to support images 
> served by Apache, thus not under the context directory.
> 
> The way I go about this now, is (Home.html):
> 
> 
>     <img jwcid="@Image" image="ognl:customerSplash"
>          src="images/acmeSplash.jpg" alt="Acme Home Page"
> 	 width="350" height="320"/>
> 
> 
> and (Home.java):
> 
> 
>     private static Location iLocation;
> 
>     // look up in database ...
>     private String getCustomerSplashImage() {
>         return "images/acmeSplash.jpg"; // just for example 
> purposes ...
>     }
> 
>     public IAsset getCustomerSplash() {
>         if (iLocation == null) {
>             ServletContext context =
>                 
> getRequestCycle().getRequestContext().getServlet().getServletC
> ontext();
>             iLocation = new Location(new 
> ContextResourceLocation(context,
>                                                               
>    "/"));
>         }
> 
>         return new ExternalAsset(getCustomerSplashImage(), iLocation);
>     }
>