如何在新行中打印JSTL迭代值

时间:2013-02-09 10:04:11

标签: java spring hibernate jsp jstl

我有一个如下字符串:

String emps =“date1,date2,date3,date4”;

我的要求是使用JSTL标签在jsp页面上打印如下:

OutPut Should be:

日期1

DATE2

DATE3

date4

我在jsp中使用了以下代码:

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

</c:forEach>

但是我的代码正在删除“,”并在下面打印如下:

date1 date2 date3 date4 

有没有人能给我一个解决方案,如何使用jstl标签在jsp中逐行打印日期值?

感谢

更新

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

注意: 我想使用<td>代码逐行打印

此(迭代)数据?

3 个答案:

答案 0 :(得分:2)

我会将String拆分为.jsp之外的List,然后将此List放入请求中。根据您周围的标记,您可以使用多种方式将项目放在新行上,例如<br/><div><p>

<强>爪哇

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>

答案 1 :(得分:2)

您的要求看起来有点奇怪,但如果您想用表格输出...

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

我希望我的问题正确无误


<强> UPD:

尝试运行下一个代码:

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

它适用于我(它输出新行中的每个字母),我相信它会对你有用。查看你的html代码并尝试运行此代码以确保它有效。


<强> UPD2:

最后你的代码看起来像这样:

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

您的错误是混合<tr>标签智慧<td>。这段代码会为每个缺席日期生成一行,它实际上是你想要的吗?


<强> upd3: 如果你想只在单元格中输出所有这些日期(看起来有点难看),请使用它:

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

答案 2 :(得分:0)

就像Kevin Bowersox指出的那样,我会在<div>

中使用<p><br /><TD>标记