You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Patrick Hubers <ph...@lists.stompbox.nl> on 2005/08/22 22:35:01 UTC

Image URL from JSF expression?

Hi,

I'm trying to put an image into a page based on the outcome of an 
expression. I've tried the following:

<h:graphicImage url="#{(authentication.role == "admin' ? 'image1.gif' : 
'image2.gif'}" />

but unfortunately, this doesn't work (the whole expression is passed 
verbatim to the rendered html), so now I'm resorting to

<h:graphicImage url="image1.gif" rendered="#{authentication.role == 
'admin'}"  />
<h:graphicImage url="image2.gif" rendered="#{authentication.role != 
'admin'}"  />

However, the above feels clumsy. Is this the right way to do it, or 
should the first option work and did I encounter a bug? Any hints 
appreciated!

(Using stock MyFaces 1.0.9 with JBoss 4.0.2 on Win2k)

-- 
Patrick Hubers

Re: Image URL from JSF expression?

Posted by Patrick Hubers <ph...@lists.stompbox.nl>.
Sean Schofield schreef:

> Your instincts are right.  This is not really the ideal way to do
> this.  You are mixing your business logic with your presentation
> logic.  Your presentation logic (JSP/JSF tags) shouldn't care what
> role you are.

I agree, but I'm still in the prototyping/mockup stage, without any real 
business logic connected.

[snip]...
> ps. Your quotation marks do not match up which is probably why your EL
> expression was failing.

Thanks, that was indeed the problem! I never noticed the single 
quotation mark; the JSP syntax coloring in JBoss IDE has some bugs, so I 
didn't see the typo. Works like a charm now.

-- 
Patrick Hubers

Re: Image URL from JSF expression?

Posted by Sean Schofield <se...@gmail.com>.
Your instincts are right.  This is not really the ideal way to do
this.  You are mixing your business logic with your presentation
logic.  Your presentation logic (JSP/JSF tags) shouldn't care what
role you are.

Instead you should delegate that kind of responsibility to your
backing bean.  Only the backing bean should know these kinds of rules.
 (Besides separation concerns, it clutters up your JSP if you leave
this kind of thing in there.)

Use something like:

<h:graphicImage id="foo"... rendered="#{authentication.displayFoo}"/>  
<h:graphicImage id="bar"... rendered="#{authentication.displayBar}"/>  

This requires the authentication bean know whom you are but this is
easily done by setting a user property based on a session variable,
etc.

IMO you don't want the URL to be determined by the bean.  Its better
to present them both in JSP and leave the URL and image stuff outside
of your backing beans (and away from your business logic.)

sean

ps. Your quotation marks do not match up which is probably why your EL
expression was failing.

On 8/22/05, Patrick Hubers <ph...@lists.stompbox.nl> wrote:
> Hi,
> 
> I'm trying to put an image into a page based on the outcome of an
> expression. I've tried the following:
> 
> <h:graphicImage url="#{(authentication.role == "admin' ? 'image1.gif' :
> 'image2.gif'}" />
> 
> but unfortunately, this doesn't work (the whole expression is passed
> verbatim to the rendered html), so now I'm resorting to
> 
> <h:graphicImage url="image1.gif" rendered="#{authentication.role ==
> 'admin'}"  />
> <h:graphicImage url="image2.gif" rendered="#{authentication.role !=
> 'admin'}"  />
> 
> However, the above feels clumsy. Is this the right way to do it, or
> should the first option work and did I encounter a bug? Any hints
> appreciated!
> 
> (Using stock MyFaces 1.0.9 with JBoss 4.0.2 on Win2k)
> 
> --
> Patrick Hubers
>

Re: Image URL from JSF expression?

Posted by Patrick Hubers <ph...@lists.stompbox.nl>.
Patrick Hubers schreef:
...

> <h:graphicImage url="#{(authentication.role == "admin' ? 'image1.gif' : 
> 'image2.gif'}" />

Sorry, that should read

<h:graphicImage url="#{(authentication.role == "admin') ? 'image1.gif' :
'image2.gif'}" />


(I only left out the right parens in my mail, not the original code...)

-- 
Patrick Hubers