1

Hello everyone I have an arraylist showing up as [MCA, MCB, COMM, DMISA] on the jsp.

Im calling it on the jsp:

<td>${bean.CodesNames}</td>

In the bean the getter is:

public void setCodesNames(ArrayList<String> CodesNames)
{
    this.CodesNames = CodesNames;
}

How can I display this without the brackets?

Doc Holiday
  • 9,928
  • 32
  • 98
  • 151

5 Answers5

6

You get the brackets because ArrayList#toString() is implicitly called, in order to turn the list into a printable string. You can fix this by printing the list yourself in the JSP:

<c:forEach items="${CodesNames}" var="item" varStatus="status">
    ${item}<c:if test="${!status.last}">,</c:if>
</c:forEach>

or with a bean getter than returns a string:

public String getCodesNamesAsString()
{
    // using a Guava Joiner
    return Joiner.on(",").useForNull("null").join(getCodesNames());
}

(See the Joiner JavaDocs if you're not familiar with Guava.)

Flexo
  • 87,323
  • 22
  • 191
  • 272
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Add another getter method:

public void getCodesNamesFormatted()
{
  StringBuilder sb = new StringBuilder();
  for (String codeName : CodesNames) sb.append(codeName).append(',');
  return sb.deleteCharAt(sb.length());
}

And call it from JSP:

<td>${bean.codesNamesFormatted}</td>
Flexo
  • 87,323
  • 22
  • 191
  • 272
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
1

You need to iterate it. Don't rely on toString.

<td>
    <c:forEach var="name" varStatus="stat" items="${bean.codesNames}">
        <c:out value="${name}"/>
        <c:if test="${stat.index < fn:length(bean.codesNames) - 1}">
            <c:out value=","/>
        </c:if>
    </c:forEach>
</td>

PS: The <c:out/> is optional here.

adarshr
  • 61,315
  • 23
  • 138
  • 167
1

You can implement getter which makes formatting you need

public String getCodesNamesFormatted()
{
    // format as you like
}

Then use it

<td>${bean.codesNamesFormatted}</td>
Flexo
  • 87,323
  • 22
  • 191
  • 272
Sergey Aslanov
  • 2,397
  • 15
  • 16
1

This string ([MCA, MCB, COMM, DMISA]) is generated by method toString() of class Array. You need to create own code which will iterate over array elements and print them one-by-one. Or of course you can use dirty way of replaceAll. See code below for both ways:

1)

<% for( String arrayItem : myArray ) { %>
<%= arrayItem %>,
<%}%>

2)

<%=myArray.toString().replaceAll("\[|\]","")%>
setec
  • 15,506
  • 3
  • 36
  • 51