systemboot
systemboot

Reputation: 862

In JSF, how can I loop through a list of values and concatenate them into a variable/param?

I have data available to me as maps and lists on a JSF page. Here is an example:

   [ 
    {
       word: "word1"
       altWord : "altWord1"
    },
    {
       word: "word2"
       altWord : "altWord2"
    },
    {
       word: "word3"
       altWord : "altWord3"
    }
   ]

I want to loop through this list:

    <ui:param name="phrase" value="" />
    <ui:repeat value="wordList" var="wordEntry">
       <ui:param name="phrase" value="#{concat(wordEntry.word)}" />
    </ui:repeat>

<!--Show phrase -->
    #{phrase}

How can I accomplish this in JSF?

Upvotes: 0

Views: 1491

Answers (1)

Michi
Michi

Reputation: 1595

I'm not 100% sure what you want to achieve but let me try. Do you mean you have a list of maps in a managed bean like this:

@SessionScoped
@ManagedBean
public class TestBean {
  private List<Map<String, String>> wordList = new ArrayList<Map<String, String>>();

  @PostConstruct
  public void init() {
    Map<String, String> wordEntry = new HashMap<String, String>();
    wordEntry.put("word", "word1");
    wordEntry.put("altWord", "altWord1");
    wordList.add(wordEntry);
    wordEntry = new HashMap<String, String>();
    wordEntry.put("word", "word2");
    wordEntry.put("altWord", "altWord2");
    wordList.add(wordEntry);
  }

  public List<Map<String, String>> getWordList() {
    return wordList;
  }
}

If yes, then concatenating the values in a view with ui:repeat is easy:

<ui:repeat var="wordEntry" value="#{testBean.wordList}" varStatus="status">
    <h:outputText value="#{wordEntry['word']}"/>
    <h:outputText value=" (#{wordEntry['altWord']})"/>
    <h:outputText value=", " rendered="#{not status.last}"/>
</ui:repeat>

This would output the following String:

word1 (altWord1), word2 (altWord2)

Upvotes: 1

Related Questions