本文介绍了If-else(嵌套)或JSTL中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a program that populates a table and depending on the type of value in each cell, I change the formatting - like percentage, decimal places, grouping etc.

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

I have a table that is the default view and I allow the user to filter those results by entering a document number and the table then changes to add 2 columns - document number and the corresponding document name.

因此,如果检索的列数是10,那么就是一些行为。如果它是12,那么,其他一些行为。

So, if the number of columns retrieved is say, 10, then some behaviour. If if it is 12, then, some other behaviour.


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

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

是默认视图的缩减版本。

is a curtailed version of the default view.


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

03/24/2013|    55       |     Waiver    |  10              |    20              | 4        | 5

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

I am using the same java program to call the queries, and populate the tables. I wrote the following in the 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列中未显示的分组。在这两种情况下,其余的列都很好。我知道它很简单:

I had first designed it with the first case in mind, and I used to just check when it got to index 0, to print the cell as is, or else, use the formatNumber. With the filtering, and the subsequent addition of the two columns, I had to rewrite the JSTL and got the second version to work perfectly, but the original one is not all awry, with grouping not appearing and a ".0" appended to the number in the 3rd column, and grouping not appearing for the 2nd column. The rest of the columns are fine in both cases. I know that its a simple:

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中没有做到这一点。我究竟做错了什么?请让我知道并提出一些改进建议。谢谢。

But I'm not getting it right in the JSTL. What am I doing wrong? Please let me know and suggest some improvements. Thank you.

推荐答案

    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>

这篇关于If-else(嵌套)或JSTL中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:02