You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Erik Gustavson <go...@gmail.com> on 2005/03/07 18:39:19 UTC

rendering an image URL

I'm working on a custom component that creates a img tag for a servlet
that returns a dynamic image.

For the servlet to create the dynamic image, I want to pass a bunch of
query params on the img tag's src URL.... what is the best way to do
this? I'm not seeing an obvious way of handling with with
ResponseWriter unless I construct the URL myself and dump it out via
the write() method.

thoughts?

-Erik

Re: rendering an image URL

Posted by Erik Gustavson <go...@gmail.com>.
thanks Heath. I got it working by doing this:


ResponseWriter writer = context.getResponseWriter();
writer.startElement("img", this);

StringBuffer src = new StringBuffer();
... create the URL, starting with a "/" but no context-root

String url = context.getApplication().getViewHandler().getResourceURL(context,
src.toString());

writer.writeURIAttribute("src",
context.getExternalContext().encodeResourceURL(url), null);

writer.endElement("img");

Re: rendering an image URL

Posted by Heath Borders <he...@gmail.com>.
It looks like the writeURIAttribute is the way to go.


On Mon, 7 Mar 2005 12:32:20 -0800, Erik Gustavson <go...@gmail.com> wrote:
> right. That's what I'm doing now, but if I try and stuff those
> parameters in with ResponseWriter.writeAttribute() they get HTML
> escaped by the writer. ResponseWriter.writeURIAttribute() seems closer
> to what I want, but I don't see how it lets me add a bunch of
> parameters to the query string.
> 
> For example, if I wanted to create a tag like <img
> src="/aServlet?p1=bar&p2=p2=foo">, I might start with something like
> this (from inside an encodeBegin method):
> 
> ---------------
> ResponseWriter writer = context.getResponseWriter();
> writer.startElement("img", this);
> 
> //write src attribute
> writer.writeAttribute("src",  "/aServlet?p1=bar&p2=p2=foo", null);
> 
> writer.endElement("img", this);
> ----------------
> 
> But this outputs:
> <img src="/aServlet?p1=bar&amp;p2=p2=foo"> -- note the SGML
> substitution for the ampersand in the middle.
> 
> Which isn't what I want.
> 
> I looked at the HtmlImageRenderer class which has some help in it for
> figuring out the web-app context root but I did not see how I could
> provide parameters to the URL.
> 
> This leaves me with the option of using the raw write() method off of
> ResponseWriter, but that strikes me as a total hack and it would
> prevent me from doing a call to super.encodeBegin() to encode all of
> the other HTML attributes like onClick, etc...
> 
> On Mon, 7 Mar 2005 13:22:47 -0600, Heath Borders
> <he...@gmail.com> wrote:
> > You can add parameters by appending their URL-encoded name-value pairs
> > after a '?'.
> >
> >
> > On Mon, 7 Mar 2005 09:39:19 -0800, Erik Gustavson <go...@gmail.com> wrote:
> > > I'm working on a custom component that creates a img tag for a servlet
> > > that returns a dynamic image.
> > >
> > > For the servlet to create the dynamic image, I want to pass a bunch of
> > > query params on the img tag's src URL.... what is the best way to do
> > > this? I'm not seeing an obvious way of handling with with
> > > ResponseWriter unless I construct the URL myself and dump it out via
> > > the write() method.
> > >
> > > thoughts?
> > >
> > > -Erik
> > >
> >
> > --
> > -Heath Borders-Wing
> > hborders@mail.win.org
> >
> 


-- 
-Heath Borders-Wing
hborders@mail.win.org

Re: rendering an image URL

Posted by Heath Borders <he...@gmail.com>.
You can add parameters by appending their URL-encoded name-value pairs
after a '?'.


On Mon, 7 Mar 2005 09:39:19 -0800, Erik Gustavson <go...@gmail.com> wrote:
> I'm working on a custom component that creates a img tag for a servlet
> that returns a dynamic image.
> 
> For the servlet to create the dynamic image, I want to pass a bunch of
> query params on the img tag's src URL.... what is the best way to do
> this? I'm not seeing an obvious way of handling with with
> ResponseWriter unless I construct the URL myself and dump it out via
> the write() method.
> 
> thoughts?
> 
> -Erik
> 


-- 
-Heath Borders-Wing
hborders@mail.win.org