If-else (nested) or when condition in JSTL

老子叫甜甜 提交于 2019-12-22 12:07:25

问题


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.

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.

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

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>

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

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.


回答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>


来源:https://stackoverflow.com/questions/22622126/if-else-nested-or-when-condition-in-jstl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!