views:

52

answers:

2

Hello,

With this code, I am able to return one image based off of a dropdown selection. Any ideas on how to return multiple images (I have attempted to create an ArrayList with the images and use the UI:Repeat tag to render it back to my view, but I was unsuccessful. Here is my current code now, which works but only returns one image. Any ideas on what approach to take to get more than one image?

Java Code:

(Person class has attribute: private String theImage;)

public String getTheImage(){

    if(this.theSchoolChoice.equals("University of Alabama")){
        theImage = "/resources/gfx/UofALogo.png";
    }

    if(this.theSchoolChoice.equals("Harvard University")){

    }
    return theImage;
}

JSF Code:

    <h:selectOneMenu value="#{person.theSchoolChoice}" style="width : 179px; height : 21px;">
        <f:selectItems value="#{person.theOptions}"/>
    </h:selectOneMenu>
    <h:outputLabel value="  "/>

    <h:commandButton action="submit" value="Get templates" style="FONT-SIZE: medium; FONT-FAMILY: 'Rockwell';width : 128px; height : 25px;">
    <f:ajax event="change" render="image" />
    </h:commandButton>
    <br></br>
    <br></br>
    <h:graphicImage id="image" value="#{person.theImage}"/>
A: 

Try to use loop as you did with ui:repeat, but use c:forEach instead. ui:repeat creates only one jsf component for h:graphicImage, and c:forEach - as many items it has. See this article for details: http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets

P.S. Your getTheImage method doesn't look nice - you should consider using IDs of images instead of names as values of your theOptions collection.

denis_k
Thanks for the reponse denis_k. I'll try the c:forEach out.
Calvin Han
Certainly don't use `c:forEach`. You can't use it in combination with ajax actions since it get executed during building of the view only, not during rendering of the view. Basically, there would be nothing anymore to render dynamically. See also my answer on [this question](http://stackoverflow.com/questions/3442380/jstl-cif-inside-a-jsf-hdatatable).
BalusC
Nice catch, I really missed that Calvin Han needs his images to be rendered in ajax.
denis_k
+1  A: 

As told in your first question about the subject, I have suggested to get hold of all images in a Map<String, List<String>> where the map key is the dropdown value and the map value is the collection of images.

Since you can't seem to figure it out, here's a kickoff example how the JSF page should look like:

<h:form>
    <h:selectOneMenu value="#{bean.groupName}">
        <f:selectItem itemValue="Please select one" />
        <f:selectItems value="#{bean.groupNames}" />
        <f:ajax event="change" render="images" />
    </h:selectOneMenu>
    <h:panelGroup id="images">
        <ui:repeat value="#{bean.images}" var="image">
            <h:graphicImage value="#{image}" /> 
        </ui:repeat>
    </h:panelGroup>
</h:form>

And here's how the associated Bean should look like:

@ManagedBean
@ViewScoped
public class Bean {

    private Map<String, List<String>> allImages = new LinkedHashMap<String, List<String>>();
    private List<String> groupNames;
    private String groupName;

    public Bean() {
        allImages.put("group1", Arrays.asList("group1a.jpg", "group1b.jpg", "group1c.jpg"));
        allImages.put("group2", Arrays.asList("group2a.jpg", "group2b.jpg", "group2c.jpg"));
        allImages.put("group3", Arrays.asList("group3a.jpg", "group3b.jpg", "group3c.jpg"));
        groupNames = new ArrayList<String>(allImages.keySet());
    }

    public List<String> getImages() {
        return allImages.get(groupName);
    }

    public List<String> getGroupNames() {
        return groupNames;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

}
BalusC
Thanks BalusC. As you can see, I am not as advanced as you are :)-C
Calvin Han
Hi BalusC, I implemented another solution for returning multiple images from a selection. I didn't want to copy your code.Just as future reference, please do not provide me with a direct code solution to the question I'm asking...I'm simply asking for a thought process answer. As you know, giving out direct answers deprives me of the learning process.I say this because from your answer, I sensed some frustration. This was not my intent.-C
Calvin Han
It was just a kickoff example to point you in the right direction. No hard feelings, I almost always post kickoff examples.
BalusC
I appreciate the response and thanks for your understanding.-C
Calvin Han
To get a better understanding what JSF is all doing under the hoods I'd suggest to go through [this article](http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html). Although the code samples are JSF 1.2 targeted, the article's text is perfectly applicable on JSF 2.0 as well.
BalusC
Thanks BalusC. I don't come from a programming background so I'm basically coming into programming cold...a friend suggested to start off learning JSF 2.0 regardless of the learning curve, since this is what is in demand in the marketplace. He got me started off using Weld, so that's where I've been building my project from.Thanks for the article.
Calvin Han