JSP-我可以使用< jsp:attribute>在< c:if>内部?例外:“必须使用jsp:body来指定标签主体" [英] JSP - Can I use <jsp:attribute> inside <c:if>? Exception: "Must use jsp:body to specify tag body"
问题描述
我在JSP中包含以下内容:
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 <MyTag if jsp:attribute is used.
如果我删除最外面的<c:if>
标记,它将起作用.在<c:if>
中使用<jsp:attribute>
是否存在问题?任何帮助,将不胜感激.谢谢.
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.
推荐答案
元素的主体被隐式定义为相应元素的主体.正文也可以使用< jsp:body>显式表示.如果一个或多个< jsp:attribute>元素出现在标签的主体中.检出元素,正文.
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.
但这不是真正的问题.问题是< jsp:attribute>不能与条件标签配合使用. < jsp:attribute>正在尝试在其父标签(在您的示例中为< c:if>)上设置属性.
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>.
您可以使用< c:if>元素内部(如BalusC在他的评论中建议的那样),但这会导致属性值为空,或者您可以从以下位置进行操作:
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>
到(更详细):
<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>
您也可以使用< c:choose>.当然,它不能与多个属性:D一起很好地工作.
You could also use a <c:choose>. And of course it won't work well with more than one attribute :D.
我个人的建议是丢弃< jsp:attribute>并找到另一种有条件地设置属性的方法.
My personal suggestion would be to throw away the <jsp:attribute> and find another way to conditionally set your attributes.
这篇关于JSP-我可以使用< jsp:attribute>在< c:if>内部?例外:“必须使用jsp:body来指定标签主体"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!