users@jersey.java.net

Re: [Jersey] Static references from JSP

From: Julio Faerman <jfaerman_at_gmail.com>
Date: Mon, 1 Sep 2008 08:45:17 -0300

I am using a simple <img> tag. Also tried the URL directly, should be the same.
I would suggest adding an static image to the bookstore sample, like a
projetct logo.
Returning a new Viewable for the image was my first attempt, but it
results in an exception saying it could not find the template.

The exception is:
java.io.IOException: The template name, line-chart.gif, could not be
resolved to the path of a template

The code:
@Path("/")
public class RootResource {
        @GET
        public Viewable getJSP(){
                return new Viewable("index.jsp",""); //This works
        }
        
        @GET
        @Path("img")
        @Produces("image/gif")
        public Viewable getImage(){
                return new Viewable("line-chart.gif",""); //This does not. Files are
in the same directory.
        }
}


Thank you for the attention and congrats all, this project is awesome.




On Mon, Sep 1, 2008 at 4:17 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
> Hi Julio,
>
> Could you send me an example?
>
>> I am using implicit JSP Viewables (just like bookstore sample), but i
>> don't know how can i reference a static resource (images, styles,
>> scripts) from the page.
>>
>
> Does the following describe your problem?
>
> If i have a resource:
>
> com.foo.Foo
>
> and an index.jsp in:
>
> com/foo/Foo/index.jsp
>
> and there is an image at the location:
>
> com/foo/Foo/image.png
>
> how does "index.jsp" reference "image.png".
>
>
>> I am trying a method that fowards to the requested resource, but
>> servletContext.getRequestDispatcher().forward() is failling because
>> the injected HttpServletRequest and Response are null. Is there a
>> better way to do this?
>>
>
> If you are using Servlet you should be able to do:
>
> @Context HttpServletRequest req;
>
> @Context HttpServletRequest res;
>
> as fields or method parameters.
>
> You should be able to reference a static page using a Viewable. Just return
> something like this:
>
> // Absolute reference
> return new Viewable("/static.png", null);
>
> // Reference relative to the resource class or a super class
> return new Viewable("static.png", null);
>
> Paul.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>