views:

89

answers:

1

I have the following inside a JSP:

<c:if test="${true}">
<jsp:attribute name="extraInlineComplianceJavascript">
window.isSummaryComplianceLinkVisible = '${TabList.isSummaryComplianceLinkVisible}';
window.isDetailComplianceLinkVisible = '${TabList.isDetailComplianceLinkVisible}';
window.complianceSummaryReportTag = '${helper.complianceSummaryReportTag}';
window.complianceDetailReportTag = '${helper.complianceReportTag}';
</jsp:attribute>
</c:if>

As is, I get the following exception:

 Must use jsp:body to specify tag body for &lt;MyTag if jsp:attribute is used.

If I remove the outermost <c:if> tags, it works. Is there a problem with using <jsp:attribute> inside a <c:if> ? Any help would be appreciated. Thanks.

+1  A: 

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.

dpb