在JSF中,如何遍历值列表并将它们连接成变量/ param?

时间:2013-04-23 15:43:05

标签: jsf loops concatenation jsf-2.2

我有数据可用作JSF页面上的地图和列表。这是一个例子:

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

我想循环浏览此列表:

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

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

如何在JSF中实现这一目标?

1 个答案:

答案 0 :(得分:1)

我不是100%肯定你想要实现的目标,但让我试试。你的意思是你有一个托管bean中的地图列表,如下所示:

@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;
  }
}

如果是,那么将视图中的值与ui:repeat连接起来很容易:

<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>

这将输出以下字符串:

word1 (altWord1), word2 (altWord2)