tags:

views:

66

answers:

1

For a uni assignment, I'm doing a JSF webapp to play poker. Cards in a player's hand are displayed by images. What's would be the "cleanest" way of mapping the card suit/rank to the URL of the associated image?

I'm currently leaning towards either using whatever Card#toString returns, or handling this in the backing bean, it's just that neither the model or the controller seems the "right" place to handle this.

(This question is somewhat subjective, but then again, so is every design question. I'm looking for some obvious solution that I missed rather than a discussion.)

+1  A: 

If you are set against producing the URL in a backing bean, you could use an EL function to do the translation:

<h:graphicImage value="#{fnlib:getImageUrlFor(myBean.cardInstance)}" />

That way, your backing beans can return the card class instance directly (I presume it is an enum) and the function will map it to an image URL. You can find an example of how to implement the function in this answer. You might need to look at FacesContext.getCurrentInstance().getExternalContext() if you want to build a URL that will be valid no matter which directory your JSP is in relative to the images (I cannot remember what rules h:graphicImage uses off the top of my head).

McDowell
Not particularly set against it, it's just that my brain keeps thinking "controller" when it sees backing beans, while this seems more like a view problem. An EL function looks like a good enough place to put this.
Sii