[英]How I can get formated string from List array in Java?
抱歉,我是Java的初學者。 我在Spring和Hibernate一起工作,但遇到了問題。 在Internet中,我找不到解決方案。
我的控制器:
List<User> theModerators = userDAO.findAllModerators();
theModel.addAttribute("moderators", theModerators); return "moderators";
moderators.jsp:
<c:forEach items="${moderators}" var="m">
<div>${m.firstName}</div>
<c:forEach items="${m.companies}" var="company">
<div>${company.compName}</div>
</c:forEach>
</c:forEach>
我對company.compName有問題。 我想用逗號來格式化字符串(例如:我有“ MicrosoftApple”,但我想-“ Microsoft,Apple”)。
我知道加入,例如:
String allCompanies = String.join(", ", userCompanies);
但是我不能將allCompanies字符串添加到Moderators中,也不能創建像theModerators這樣的帶有allCompanies字符串的新數組。
我曾經使用過PHP / Laravel,在那里我使用了加入集合的連接。
但是在Java中我做不到。
以下應該工作
<c:forEach items="${moderators}" var="m">
<div>${m.firstName}</div>
<div>
<c:forEach items="${m.companies}" var="company" varStatus="item">
${company.compName}
<c:if test="${!item.last}">,</c:if>
</c:forEach>
</div>
</c:forEach>
我已經使用varStatus避免了最后一個逗號
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.