LoginSignup
0
0

More than 3 years have passed since last update.

JSFで文字列のリストをカンマ区切りの文字列で出力する方法

Posted at
  • 環境
    • CentOS Linux release 7.8.2003 (Core)
    • openjdk version "11.0.7" 2020-04-14 LTS
    • JSF 2.3.9

スクリーンショット 2020-09-24 22.48.35.png

方法1 : Beabでカンマ区切りの文字列用にGettrを作る

参考 : Javaでカンマ区切りで文字列連結するいくつかの方法 - Qiita

xhtml
<!--省略-->
<h5>文字列のリストをカンマ区切りの文字列で出力する</h5>
<h:outputText value="#{sampleBean.commaDelimitedString}" />
<!--省略-->
SampleBean.java
// 省略
/** 文字列のリスト. */
@Getter
private List<String> strings = Arrays.asList("文字列の", "リストを", "カンマ区切りで", "表示したい。");

/**
 * 文字列のリストをカンマ区切りの文字列として取得する.
 * @return カンマ区切りの文字列
 */
public String getCommaDelimitedString() {
    return this.strings.stream().collect(Collectors.joining(","));
}
// 省略

方法2 : xhtmlでui:repeatする

xhtml
<!--省略-->
<ui:repeat var="string" value="#{sampleBean.strings}" varStatus="index">
  <h:outputText value="#{string}" />
  <ui:fragment rendered="#{!index.last}">,</ui:fragment>
</ui:repeat>
<!--省略-->
SampleBean.java
// 省略
/** 文字列のリスト. */
@Getter
private List<String> strings = Arrays.asList("文字列の", "リストを", "カンマ区切りで", "表示したい。");
// 省略
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0