你不应该这样做吗?
<c:choose>
<c:when test="${i.tag = 'th'}"><th></c:when>
<c:when test="${i.tag = 'td'}"><td></c:when>
</c:choose>
<!-- 100 lines of code -->
<c:choose>
<c:when test="${i.tag = 'th'}"></th></c:when>
<c:when test="${i.tag = 'td'}"></td></c:when>
</c:choose>
如果您希望您的标记是有效的 XML;使用一个
自定义标签来包装您想要避免重复的所有 Java 代码。然后你的标记看起来像
<c:choose>
<c:when test="${i.tag = 'th'}">
<th>
<my:customTag anyAttributes="th-related-values-if-any" ... />
</th>
</c:when>
<c:when test="${i.tag = 'td'}">
<td>
<my:customTag anyAttributes="td-related-values-if-any" ... />
</td>
</c:when>
</c:choose>
或者,可以使用以下 hack(
@Uooo建议)来传递 XML 验证器。
<c:choose>
<c:when test="${i.tag = 'th'}"><c:out value="<th>%" /></c:when>
<c:when test="${i.tag = 'td'}"><c:out value="<td>%" /></c:when>
</c:choose>
<!-- 100 lines of code -->
<c:choose>
<c:when test="${i.tag = 'th'}"><c:out value="</th>%" /></c:when>
<c:when test="${i.tag = 'td'}"><c:out value="</td>%" /></c:when>
</c:choose>