Search code examples
javajsparraylistjsp-tags

Need ArrayList printed without "Brackets"


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?


Solution

  • 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.)