简体   繁体   English

如何从Java中的列表数组获取格式化的字符串?

[英]How I can get formated string from List array in Java?

Sorry, I'm beginner in Java. 抱歉,我是Java的初学者。 I work with Spring and Hibernate and I have problem. 我在Spring和Hibernate一起工作,但遇到了问题。 In Internet I can't find solution. 在Internet中,我找不到解决方案。

My Controller: 我的控制器:

List<User> theModerators = userDAO.findAllModerators();

theModel.addAttribute("moderators", theModerators); return "moderators";

moderators.jsp: 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>

I have problem with with company.compName. 我对company.compName有问题。 I want to get formated string with commas (ex: I got "MicrosoftApple", but I want - "Microsoft, Apple"). 我想用逗号来格式化字符串(例如:我有“ MicrosoftApple”,但我想-“ Microsoft,Apple”)。

I know about join, for ex: 我知道加入,例如:

String allCompanies = String.join(", ", userCompanies);

But I can't add allCompanies string to theModerators, or create new array like theModerators but with allCompanies string. 但是我不能将allCompanies字符串添加到Moderators中,也不能创建像theModerators这样的带有allCompanies字符串的新数组。

I have worked with PHP/Laravel, and there I used joins into collections. 我曾经使用过PHP / Laravel,在那里我使用了加入集合的连接。

But in Java I can't do it. 但是在Java中我做不到。

The following should work 以下应该工作

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

I have made use of varStatus to avoid the last comma 我已经使用varStatus避免了最后一个逗号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM