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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14786957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:23:45  来源:igfitidea点击:

How to Print JSTL iteration values in a new line

javaspringhibernatejspjstl

提问by String

I have a String like below:

我有一个像下面这样的字符串:

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

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

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

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

OutPut Should be:

date1

日期1

date2

日期2

date3

日期3

date4

日期 4

I have used the below code in my jsp:

我在我的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?

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

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) 数据逐行使用 <td><td>标签?

回答by Kevin Bowersox

I would split the Stringinto a Listoutside of your .jsp, then place this Listinto 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>.

我会将其拆分StringList.jsp的外部,然后将其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

JSTL

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

回答by bsiamionau

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.

它对我来说很好用(它在新行中输出每个字母),我相信它对你有用。检查您的 html 代码并尝试运行此代码以确保它有效。



upd2:

更新2:

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?

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



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

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>

回答by Horizon

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

就像 Kevin Bowersox 指出的那样,我会在里面使用<div>or<p><br />标签<TD>