JSTL <c:choose> if标签

今天上午为了项目统一要把页面中的struts标签改为jstl标签。

简要记录一下修改过程:用到标签的地方主要是遍历和条件判断。其他地方能用el表达式代替的尽量用el表达式。


struts遍历:<s:iterator value="userCourseBeans" var="userCourse">

                      <td align="center"><s:property value="#c.courseID.formID.formName" /></td>


                      </s:iterator>

jstl遍历和struts差不多:<c:forEach var="userCourse" items="${userCourseBeans}"> </c:forEach>





struts条件判断:<s:if test="#userCourse.courseID.bookDinner==true">
                                 <s:if test="#userCourse.bookDinner==true">
                                    结果一
                                </s:if>
                                 <s:else>
                                     结果二
                                </s:else>
                             </s:if>
                             <s:else>
                                结果三
                            </s:else>

但是用jstl就感觉麻烦一些:<c:choose>
                                 <c:when test="${userCourse.courseID.bookDinner}">
                                     <c:choose>
                                         <c:when test="${userCourse.bookDinner}">
                                             结果一
                                        </c:when>
                                         <c:otherwise>
                                            结果二
                                        </c:otherwise>
                                     </c:choose>
                                 </c:when>
                                 <c:otherwise>
                                     结果三
                                </c:otherwise>
                             </c:choose>

也就是说jstl必须用<c:wher>和<c:otherwise>才能实现if else操作。用<c:if>只能实现if操作。

并且if else出现嵌套时,如上例每个相当于每个<c:otherwise>外面都要有<c:choose>.其实<c:when>和<c:otherwise>相当于java中的switch中的case default,而<c:choose>相当于switch;

猜你喜欢

转载自ymq267.iteye.com/blog/2264265