String
String

Reputation: 3728

How to Print JSTL iteration values in a new line

I have a String like below:

String emps="date1,date2,date3,date4";

My requirement is to print like below on a jsp page using JSTL tags:

OutPut Should be:

date1

date2

date3

date4

I have used the below code in my jsp:

<c:set var="string2" value="${fn:split(emps,',')}" />
<c:forEach items="${string2}" var="emps1">
<td><c:out value="${emps1}"/></td>

</c:forEach>

But my code is removing "," this and printing like below in a single line:

date1 date2 date3 date4 

Could any one give me a solution that ,how to print the date values line by line in a jsp using jstl tags?

thanks

Update:

<c:when test="${requestScope.size!=0}">
        <table border="1">
            <tr>
                <th>Employee Code</th>

                <th>EmployeeName</th>
                <th>EmployeeDepartment</th>
                <th>AbsentDate</th>
                <th>TotalNOOfAbsentDates</th>
            </tr>


            <c:forEach items="${requestScope.set1}" var="emps">
                <tr>
                    <td><c:out value="${emps[0]}" /></td>

                    <td><c:out value="${emps[1]}" /></td>
                    <td><c:out value="${emps[2]}" /></td>

                    <td><c:out value="${emps[4]}" /></td>
                    <c:set var="string2" value="${fn:split(emps[3],',')}" />
                    <c:forEach items="${string2}" var="emps1">
                    <td>
                        <p><c:out value="${emps1}"/></p>

                        </td>

                    </c:forEach>

                    </tr>
            </c:forEach>

Note: I want to print

this(Iteration) data line by line using <td> tag?

Upvotes: 0

Views: 13054

Answers (3)

Horizon
Horizon

Reputation: 548

Just like, Kevin Bowersox pointed out, I would use either a <div> or <p> or <br /> tag inside the <TD>

Upvotes: 0

bsiamionau
bsiamionau

Reputation: 8229

Your requirements looks little strange, but if you want to output it with table...

<table>
    <c:set var="string2" value="${fn:split(emps,',')}" />
    <c:forEach items="${string2}" var="emps1">
        <tr>
            <td><c:out value="${emps1}"/></td>
        </tr>
    </c:forEach>
</table>

I hope I got your question correctly


upd:

Try to run next code:

<table>
    <c:set var="sourceString" value="${fn:split('q,w,e,r,t,y',',')}" />
    <c:forEach items="${sourceString}" var="element">
        <tr>
            <td><c:out value="${element}"/></td>
        </tr>
    </c:forEach>
</table> 

It works fine for me (it outputs each letter in new row) and I sure that it would work for you. Check out your html code and try to run this code to be sure, that it works.


upd2:

Finally your code would look like this:

<table border="1">
    <tr>
        <th>Employee Code</th>

        <th>EmployeeName</th>
        <th>EmployeeDepartment</th>
        <th>AbsentDate</th>
        <th>TotalNOOfAbsentDates</th>
    </tr>


    <c:forEach items="${requestScope.set1}" var="emps">
        <tr>
            <c:set var="string2" value="${fn:split(emps[3],',')}" />
            <c:forEach items="${string2}" var="emps1">

                <td><c:out value="${emps[0]}" /></td>
                <td><c:out value="${emps[1]}" /></td>
                <td><c:out value="${emps[2]}" /></td>
                <td><c:out value="${emps1}"/></td>
                <td><c:out value="${emps[4]}" /></td>

            </c:forEach>
        </tr>
    </c:forEach>
</table>

Your mistake was to mix <tr> tags wits <td>. This code would generate row for each absent date, is it actually what you want?


upd3: If you want to output all this dates in only cell (it looks little ugly), use it:

<table border="1">
    <tr>
        <th>Employee Code</th>

        <th>EmployeeName</th>
        <th>EmployeeDepartment</th>
        <th>AbsentDate</th>
        <th>TotalNOOfAbsentDates</th>
    </tr>


    <c:forEach items="${requestScope.set1}" var="emps">
        <tr>
            <td><c:out value="${emps[0]}" /></td>
            <td><c:out value="${emps[1]}" /></td>
            <td><c:out value="${emps[2]}" /></td>
            <td>
                <c:set var="string2" value="${fn:split(emps[3],',')}" />
                <c:forEach items="${string2}" var="emps1">
                    <p>
                        <c:out value="${emps1}"/>
                    </p>
                </c:forEach>
            </td>
            <td><c:out value="${emps[4]}" /></td>
        </tr>
    </c:forEach>
</table>

Upvotes: 2

Kevin Bowersox
Kevin Bowersox

Reputation: 94479

I would split the String into a List outside of your .jsp, then place this List into the request. Depending on your surrounding markup you can use a number of ways to put the items on a new line such as <br/>, <div> or <p>.

Java

String emps="date1,date2,date3,date4";
List<String> empList = new ArrayList<String>(Arrays.asList(emps.split(",")));
request.setAttribute("empList", empList);

JSTL

<c:forEach items="${empList}" var="emp">
   <div><c:out value="${emp}"/></div>
</c:forEach>

Upvotes: 2

Related Questions