If-else(嵌套)或JSTL中的条件

时间:2014-03-24 22:45:18

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

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

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

因此,如果检索到的列数是10,则表示某些行为。如果它是12,那么,其他一些行为。

Date       |   Documents Bought | Documents Started  | Column 4    | Column 5

03/24/2013 |   1000.0           |    2,000           |    1,500    | 2,500

是默认视图的缩减版本。

Date      |  Document ID| Document Name | Documents Bought | Documents Started  | Column 4 | Column 5

03/24/2013|    55       |     Waiver    |  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中没有把它弄好。我究竟做错了什么?请让我知道并提出一些改进建议。谢谢。

1 个答案:

答案 0 :(得分: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>