【问题标题】:In JSF, how can I loop through a list of values and concatenate them into a variable/param?在 JSF 中,如何循环遍历值列表并将它们连接到变量/参数中?
【发布时间】:2013-04-23 15:43:05
【问题描述】:

我有可用的数据作为 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 中做到这一点?

【问题讨论】:

    标签: jsf loops concatenation jsf-2.2


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多