JSP - Can I use <jsp:attribute> inside <c:if>? Exception: “Must use jsp:body to specify tag body”

一曲冷凌霜 提交于 2019-12-05 06:58:14

The body of an element is defined implicitly as the body of the the respective element. The body can also be represented explicitly using <jsp:body>. This is required if one or more <jsp:attribute> elements appear in the body of the tag. Checkout the references for element, attribute and body.

But that is not the real problem. The problem is <jsp:attribute> does not play well with conditional tags. The <jsp:attribute> is trying to set the attribute on its parent tag, which in your example is <c:if>.

You could use <c:if> inside the element (as BalusC suggested in his comment), but that will result in a an attribute with an empty value, or you could go from:

<jsp:element ...>
  <c:if test="${true}">
    <jsp:attribute name="extraInlineComplianceJavascript">
      ....
    </jsp:attribute>
  </c:if>
</jsp:element>

to (a more verbose):

<c:if test="${true}">
  <jsp:element ...>
    <jsp:attribute name="extraInlineComplianceJavascript">
      ....
    </jsp:attribute>
  </jsp:element>
</c:if>
<c:if test="${false}">
  <jsp:element ...>
     <!-- no attribute for false -->
  </jsp:element>
</c:if>

You could also use a <c:choose>. And of course it won't work well with more than one attribute :D.

My personal suggestion would be to throw away the <jsp:attribute> and find another way to conditionally set your attributes.

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