4
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<td colspan="1" width="100">
<c:choose>
  <c:when>
  </c:when>

  <c:when>
  </c:when>

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

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

在选择中嵌套选择是否有限制,例如这样?

编辑:JSP 编译器一直抱怨我没有结束标记,如果我在 else 中放置另一个 <c:when></c:when>。

4

4 回答 4

5

据我所知有一个错误:

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

应该:

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

您不能直接在内部嵌套 when 标记,为此您需要另一个选择标记:

<c:when>
    <c:choose>
        <c:when></c:when>
        <c:otherwise>
            <c:choose>
                <c:when></c:when>
            </c:choose>
        </c:otherwise>
    </c:choose>
</c:when>
于 2014-03-28T11:46:06.330 回答
2

JSP 将被翻译为 Java,其中将为每个自定义标签创建函数,如下所示

private boolean _jspx_meth_c_005fwhen_005f0(javax.servlet.jsp.tagext.JspTag _jspx_th_c_005fchoose_005f0, javax.servlet.jsp.PageContext _jspx_page_context)
      throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
//  c:when
org.apache.taglibs.standard.tag.rt.core.WhenTag _jspx_th_c_005fwhen_005f0 = (org.apache.taglibs.standard.tag.rt.core.WhenTag) _005fjspx_005ftagPool_005fc_005fwhen_0026_005ftest.get(org.apache.taglibs.standard.tag.rt.core.WhenTag.class);
_jspx_th_c_005fwhen_005f0.setPageContext(_jspx_page_context);
_jspx_th_c_005fwhen_005f0.setParent((javax.servlet.jsp.tagext.Tag) _jspx_th_c_005fchoose_005f0);
// /index1.jsp(4,2) name = test type = boolean reqTime = true required = true fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fwhen_005f0.setTest(((java.lang.Boolean) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${true}", java.lang.Boolean.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false)).booleanValue());
int _jspx_eval_c_005fwhen_005f0 = _jspx_th_c_005fwhen_005f0.doStartTag();
if (_jspx_eval_c_005fwhen_005f0 != javax.servlet.jsp.tagext.Tag.SKIP_BODY) {
  do {
    out.write("\r\n");
    out.write("  ");
    int evalDoAfterBody = _jspx_th_c_005fwhen_005f0.doAfterBody();
    if (evalDoAfterBody != javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN)
      break;
  } while (true);
}
if (_jspx_th_c_005fwhen_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
  _005fjspx_005ftagPool_005fc_005fwhen_0026_005ftest.reuse(_jspx_th_c_005fwhen_005f0);
  return true;
}
_005fjspx_005ftagPool_005fc_005fwhen_0026_005ftest.reuse(_jspx_th_c_005fwhen_005f0);
return false;
}

拥有嵌套函数将增加函数堆栈级别。默认情况下,JVM 具有不错的堆栈大小,并且应该会给您带来任何问题/

于 2012-06-27T06:05:37.800 回答
1

最终你可能会用完堆栈空间。否则没有。

于 2012-06-26T21:38:03.810 回答
1

常识和可读性是您可能遇到的最接近的限制。你有这个代码的一些问题吗?

于 2012-06-26T21:38:44.777 回答