2013-02-09 59 views
0

我有一个如下所示的字符串:如何在一个新行中打印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 

可以在任何一个给我一个解决方案,如何通过线在打印的日期值线一个jsp使用jstl标签?

感谢

更新:

<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>标记线线?

+0

您为什么使用'​​'标签? ''标签内是否有''? – 2013-02-09 10:08:34

+0

我想打印表格格式的数据,所以我需要使用​​.so我如何使用
和​​标签 – String 2013-02-09 10:10:41

回答

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> 

我希望我能得到你的答案estion正确


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

我已经更新了我的问题,如果我跑侑代码,我能够显示迭代值在单行 – String 2013-02-09 10:33:53

+0

@Kumar,已更新 – bsiamionau 2013-02-09 10:53:14

+0

好吧我会尽量让你知道 – String 2013-02-09 10:54:32

2

我会将String拆分为您的.jsp外部的List,然后将此List放入请求中。根据周围的标记,您可以使用多种方法将项目放置在新线路上,如<br/>,<div><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> 
+0

我想打印数据表格式,因此我需要使用​​。所以我如何使用的
等等与​​标签 – String 2013-02-09 10:18:54

+0

​​ \t \t \t \t \t \t

\t \t \t \t \t \t \t \t \t \t \t \t这样它不工作 – String 2013-02-09 10:19:30

+0

@Kumar张贴周围的HTML – 2013-02-09 10:22:07

0

就像凯文·鲍尔索克斯指出的那样,我会请使用<div><p><br />标签<TD>