2010-12-22 87 views
8

我遇到以下教程JSP tricks to make templating easier?,它使用JSP创建页面模板(我怎么错过了这么久?!?)。但是,在做了一些搜索之后,我似乎无法弄清楚如何(或者如果有可能)检查是否设置了JSP片段。检查JSP片段是否已设置

这里是什么,我试图做一个例子:

我有一个名为default.tag模板。它具有定义2个JSP属性如下:

<%@attribute name="title" fragment="true" required="true" %> 
<%@attribute name="heading" fragment="true" %> 

然后,在页面的代码,我已经设置为<jsp:invoke fragment="title" />页面<title>元件。再后来下来的页面,我有以下几点:

<c:choose> 
    <c:when test="${true}"> 
     <jsp:invoke fragment="heading" /> 
    </c:when> 
    <c:otherwise> 
     <jsp:invoke fragment="title" /> 
    </c:otherwise> 
</c:choose> 

在那里我有<c:when test="${true}">,我希望能够以检查heading片段是为了显示它已定,但如果没有,则默认为title片段。

回答

11

在做了更多的混乱之后,我会回答我自己的问题。事实证明attribute的名字实际上也变成了一个变量。因此,我可以执行以下操作:

<c:choose> 
    <c:when test="${not empty heading}"> 
     <jsp:invoke fragment="heading" /> 
    </c:when> 
    <c:otherwise> 
     <jsp:invoke fragment="title" /> 
    </c:otherwise> 
</c:choose> 

这就是我需要做的。好像我只是想让它比需要的更难!