...">

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

I have inside 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 external <c:if> tags, it works. Is there a problem using <jsp:attribute> inside a <c:if> ? Any help would be greatly appreciated. Thanks.

+6
jsp jsp-tags jspinclude
source share
1 answer

The body of an element is implicitly defined as the body of the corresponding element. A body can also be explicitly represented using <jsp: body>. This is necessary if one or more <jsp:> attributes of the elements appear in the body of the tag. Link to an element , attribute, and body .

But this is not a real problem. The problem is <jsp: attribute> doesn't work well with conditional tags. The <jsp:> attribute is trying to set the attribute in its parent tag, which in your example is <c: if>.

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

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

(more detailed):

 <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 can also use <c: choose>. And of course, this will not work with more than one attribute: D.

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

+5
source share

All Articles