【问题标题】:If-else (nested) or when condition in JSTLJSTL 中的 if-else(嵌套)或 when 条件
【发布时间】:2014-03-24 22:45:17
【问题描述】:

我有一个填充表格的程序,根据每个单元格中的值类型,我会更改格式 - 例如百分比、小数位、分组等。

我有一个默认视图的表格,我允许用户通过输入文档编号来过滤这些结果,然后表格更改为添加 2 列 - 文档编号和相应的文档名称。

所以,如果检索到的列数是 10,那么就会出现一些行为。如果它是 12,那么,其他一些行为。

日期 |购买的文件 |文件开始 |第 4 栏 |第 5 栏 2013 年 3 月 24 日 | 1000.0 | 2,000 | 1,500 | 2,500

是默认视图的缩减版。

日期 |文件编号|文件名称 |购买的文件 |文件开始 |第 4 栏 |第 5 栏 2013 年 3 月 24 日| 55 |弃权 | 10 | 20 | 4 | 5

我正在使用相同的 java 程序来调用查询并填充表。我在 JSP 中写了以下内容。

  <c:forEach var="row" items="${docFunnel}">
                            <tr>
                                <c:forEach var="cell" items="${row}"
                    varStatus="rowIdx">

                        <c:choose>

                        <c:when
                            test="${rowIdx.count==17}">

                                        </c:when>
                                        <c:when
                            test="${rowIdx.index==2}">
                                            <td>${cell}</td>
                                        </c:when>

                                        <c:when
                            test="${rowIdx.index==1}">
                                            <td><fmt:formatNumber
                                    type="number" maxFractionDigits="3" groupingUsed="false"
                                    value="${cell}" /></td>
                                        </c:when>

                                        <c:otherwise>
                                        <c:choose>

                                        <c:when
                                    test="${rowIdx.index==0}">
                                            <td>${cell}</td>
                                        </c:when>

                                        <c:otherwise>

                                                <td>
                                                    <fmt:formatNumber
                                            type="number" maxFractionDigits="3" groupingUsed="true"
                                            value="${cell}" />
                                                </td>


                                            </c:otherwise>
                                            </c:choose>
                                             </c:otherwise>

                                    </c:choose>

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

我最初设计它时考虑了第一种情况,我过去只是检查它何时到达索引 0,按原样打印单元格,否则,使用 formatNumber。通过过滤以及随后添加的两列,我不得不重写 JSTL 并让第二个版本完美运行,但原来的版本并非全是错误的,没有出现分组,并且在后面附加了一个“.0”第 3 列中的数字,并且第 2 列未出现分组。在这两种情况下,其余列都很好。我知道这很简单:

if(no. of columns == 12)
   if(index == 0 || index == 2)
       no format   //print date and name of document as is
  if(index == 1)
      numberFormat with no grouping used //since this indicates document ID

else if(no. of columns == 10)
  if(index == 0)
     no format  //print date as is

else
  numberFormat with grouping //regardless of the number of columns

但我在 JSTL 中没有正确处理。我究竟做错了什么?请让我知道并提出一些改进建议。谢谢。

【问题讨论】:

  • 你做错了什么?
  • 第 2 列和第 3 列中的分组以及将“.0”附加到第 3 列中的数字的事实是不可取的。这仅在第一种情况下发生。过滤后,它可以正确呈现。

标签: jsp if-statement for-loop jstl case-when


【解决方案1】:
    if(no. of columns == 12)
       if(index == 0 || index == 2)
           no format   //print date and name of document as is
      if(index == 1)
          numberFormat with no grouping used //since this indicates document ID

    else if(no. of columns == 10)
      if(index == 0)
         no format  //print date as is

    else
      numberFormat with grouping //regardless of the number of columns


=========================================================

you have to write it as below

    <c:choose>
    <c:when test="${rowIdx.count==12}">
    <c:if test="${rowIdx.index==0} || ${rowIdx.index==2}">
      no format   //print date and name of document as is
    </c:if>
    <c:if test="${rowIdx.index==1}">
       numberFormat with no grouping used //since this indicates document ID
    </c:if>
    </c:when>
    <c:when test="${rowIdx.count==10}">
    <c:if test="${rowIdx.index==0}">
       no format  //print date as is
    </c:if>
    </c:when>`enter code here`
    <c:otherwise>
     numberFormat with grouping //regardless of the number of columns
     </c:otherwise>

【讨论】:

    猜你喜欢
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    相关资源
    最近更新 更多