JSF switching image
This is a follow-up to question Using <c:when> with an enumeration
After following the suggestion from the accepted answer, I encountered another problem. The real image path (my code was example code) is partially generated using a library function to get a themed image path. Please understand that I'm not allowed to disclose real code, so I have to cut lots of pieces when I post here.
The first problem is that h:graphicImage automatically generates an absolute image path for the img tag using the current portlet's path. Example: I need http://myserver/mytheme/portlet/image.gif but if I use <h:graphicsImage value="#{myNs:getThemePath()}image.gif" />
it gets rendered as http://myserver/myportlet/mytheme/portlet/image.gif; instead, using <img src="#{myNs:getThemePath()}image.gif" />
works perfectly.
As specified in the other question, I need to switch between images basing on a condition of an iterating item in an ICEFaces table, AND I also need (for accessibility reasons) to place a correct alt
attribute with a localized resource.
Briefly, my code resembles the following (ice:dataTable
omitted)
<h:column>
<f:facet name="header">
<ice:outputText value="#{myNs:getLocale('state')}" />
</f:facet>
<img alt="[[TODO]]" src="#{myNs:getThemePath()}#{item.state == 'COMPLETED' ? 'ok' : 'ko'}.gif"
</h:column>
The src part works, but now come the problems.
If I use alt="#myNs:getLocale('prefix.#{item.state = 'COMPLETED' ? 'completed' : 'canceled'}')}
I can't nest expressions (it doesn't get evaluated)
If I create two <h:graphicsImage
tags for the two cases (no need to use complex expressions in alt
) and use the rendered
attribute as described in the other question's accepted answer, I don't get the image URL rendered correctly.
I also can't use the <c:when
tag because, as explained in the other question, it gets always evaluated to false.
What can I do to meet both requirements? How can I 开发者_如何学JAVAuse two <img
tags and switch their rendering according to my condition?
Thanks
[add] Fortunately, I got the following exception message on Tomcat logs when combining expression . Can somebody help me learn EL expressions better? I'm totally new to JSF
Caused by: org.apache.el.parser.ParseException: Encountered "#" at line 1, column 32.
Was expecting one of:
<INTEGER_LITERAL> ...
<FLOATING_POINT_LITERAL> ...
<STRING_LITERAL> ...
"true" ...
"false" ...
"null" ...
"(" ...
"!" ...
"not" ...
"empty" ...
"-" ...
<IDENTIFIER> ...
<FUNCTION_CALL> ...
To answer the concrete question of the ability to render plain vanilla HTML conditionally; just wrap them in a JSF component which supports the rendered
attribute but does by itself output nothing.
<h:panelGroup rendered="#{item.state == 'COMPLETED'}">
<img src="ok.gif" />
</h:panelGroup>
<h:panelGroup rendered="#{item.state == 'CANCELLED'}">
<img src="ko.gif" />
</h:panelGroup>
You can also use <ui:fragment>
instead when you're using Facelets. It has a bit less overhead.
Looks like I solved it by myself in 10 minutes. Let me share how.
A combined expression is feasible, but simply needs no #{} again!!!! What a dumb (did I say I'm a total beginner?)
#{item.state = 'COMPLETED' ? myNs:getLocale('prefix.completed') myNs:getLocale('prefix.canceled')}
works
精彩评论